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

ModelSerializer default querysets #5358

Closed
bobort opened this issue Aug 23, 2017 · 3 comments
Closed

ModelSerializer default querysets #5358

bobort opened this issue Aug 23, 2017 · 3 comments

Comments

@bobort
Copy link

bobort commented Aug 23, 2017

Some of my ForeignKeys in my models use the limit_choices_to argument. I was a bit surprised to find that the auto-generated forms did not follow the same rules, so I find myself overriding the queryset argument used in my custom ModelSerializer class.

I've never contributed source code to a project, and I really don't know how to do it, but I would like to propose the following replacement to rest_framework/relations.py, line 158:

            if hasattr(self.parent, "Meta") and hasattr(self.parent.Meta, "model"):
                queryset = queryset.filter(
                    **self.parent.Meta.model._meta.get_field(self.source).get_limit_choices_to()
                )
            else:
                queryset = queryset.all()

Truth be told, there's a comment in there about repr output, and my worthless opinion is that you have an incredibly bizarre reason to pass around Managers from the ModelSerializer instead of QuerySets. Isn't there a way to obtain the manager from a QuerySet object so that you can create your own custom repr for them if they're really that important for everyone? But, I digress...

@rpkilby
Copy link
Member

rpkilby commented Aug 23, 2017

Here's a fixed link to the relevant method.

@tomchristie
Copy link
Member

Okay, we could accept a pull request along those lines, and start by seeing if breaks any of the existing tests or not.

@carltongibson
Copy link
Collaborator

This is a duplicate of #1811. Closing as such.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants