Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for limit_choices_to on related fields. #1843

Conversation

pipermerriam
Copy link
Contributor

Refs #1811.

What is the problem / feature ?

Serializer fields representing relationships did not support or respect the limit_choices_to option as introduced from django's ForeignKey field.

How did it get fixed / implemented ?

  • Added a new keyword argument limit_choices_to to rest_framework.relations.RelatedField
  • Added support for extracting this value from the model field when auto generating a field.
  • Added support for looking this field up from the model in the appropriate situations.

How can someone test / see it ?

Test coverage, as well as creating a related field using the limit_choices_to argument and seeing that the resulting queryset used for the field is correctly filtered.

Here is a cute animal picture for your troubles...

o-monkeys-ride-dogs-570

@tomchristie tomchristie modified the milestone: 3.0.2 Release Dec 11, 2014
@tomchristie tomchristie changed the title Add support for limit_choices_to on related fields fixing #1811 Add support for limit_choices_to on related fields. Dec 17, 2014
@tomchristie tomchristie modified the milestones: 3.2.0 Release, 3.3.0 Jun 3, 2015
@@ -846,6 +843,9 @@ def get_related_field(self, model_field, related_model, to_many):
if model_field.help_text is not None:
kwargs['help_text'] = model_field.help_text

if model_field.rel.limit_choices_to:
kwargs['limit_choices_to'] = model_field.rel.limit_choices_to
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not clear about whether it should be related_model instead of model_field.rel here. Thoughts ?

@thedrow
Copy link
Contributor

thedrow commented Jun 13, 2015

@pipermerriam This PR should be rebased.

# GenericForeignKey and their reverse relationships don't have
# a `field` property.
pass

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what this code block is for. self.limit_choices_to is only relevant when queryset is not None so why do we need to set it to anything here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the event that the model field is defined as:

class MyModel(models.Model):
    user = models.ForeignKey(User, limit_choices_to={'is_active': True})

it seemed appropriate for DRF to respect that value and filter the default queryset.

Do you think it would be better to only do filtering when limit_choices_to is explicitly declared on the serializer field?

@pipermerriam
Copy link
Contributor Author

This PR is not rebasing easily. Sorry for the long turn-around time.

@tomchristie
Copy link
Member

Not a problem, still on my radar so will get to it myself too if you don't first. Dropping the 3.3.0 milestone, but mostly just because there's no reason it can't be before then.

@tomchristie tomchristie removed this from the 3.3.0 Release milestone Jul 31, 2015
@pipermerriam
Copy link
Contributor Author

Thanks @tomchristie . I ended up in rebase hell trying to rebase this. A lot of the surrounding code has changed since it was originally written. I think I'm going to step back from this feature for now as I don't have much time to put towards things. Feel free to take it an run with it if you see fit.

@auvipy
Copy link
Member

auvipy commented Dec 20, 2015

@pipermerriam I would like to take charge of this change if you step back.

@pipermerriam
Copy link
Contributor Author

It's all yours. My focus is currently elsewhere

On Sun, Dec 20, 2015, 12:47 AM Asif Saifuddin Auvi notifications@github.com
wrote:

@pipermerriam https://github.com/pipermerriam I would like to take
charge of this change if you step back.


Reply to this email directly or view it on GitHub
#1843 (comment)
.

@mishu-
Copy link

mishu- commented Mar 6, 2017

@tomchristie is the method & approach ok? I can fix the conflicts if that's all that's needed.

@tomchristie
Copy link
Member

@mishu- Seems broadly right yup, there'll probably need to be some more thorough review following the conflict being resolved, but the general approach is 👌.

@mishu-
Copy link

mishu- commented Mar 6, 2017 via email

@auvipy
Copy link
Member

auvipy commented Mar 6, 2017

yes I also got that while tried to takeover

@xordoquy
Copy link
Collaborator

xordoquy commented Mar 6, 2017

That's likely, however, with #3605 things may be easier.

@tomchristie
Copy link
Member

Closing this off as stale.

@tomchristie tomchristie closed this May 2, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants