Discussion:
Limit queryset in field
Jakob Damgaard Møller
2018-06-12 18:19:39 UTC
Permalink
Hello,
if i have a model like:

class Model1(models.Model):
name = models.CharField()
owner = models.ForeignKey(User)

class Model2(models.Model):
name = models.CharField()
model2 = models.ForeignKey(Model2)
owner = models.ForeignKey(User)

And i want to limit the queryset in my serializer, so that when creating
Model2, the only allowed "model2-objects" is the one that will match this
query:

Model2.objects.filter(owner=CURRENT_USER)

Is the right way to do it like this in __init__()

self.fields['model2'].queryset = Models2.objects.filter(owner=CURRENT_USER)
or is there a better way to solve this ?
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Carlton Gibson
2018-06-12 19:24:06 UTC
Permalink
Hi Jakob.

That looks about right. 🙂

C.
Post by Jakob Damgaard Møller
Hello,
name = models.CharField()
owner = models.ForeignKey(User)
name = models.CharField()
model2 = models.ForeignKey(Model2)
owner = models.ForeignKey(User)
Model2.objects.filter(owner=CURRENT_USER)
Is the right way to do it like this in __init__()
self.fields['model2'].queryset = Models2.objects.filter(owner=CURRENT_USER) or is there a better way to solve this ?
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
For more options, visit https://groups.google.com/d/optout <https://groups.google.com/d/optout>.
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Sadialiou Diallo
2018-06-25 10:28:37 UTC
Permalink
for me the most way is to specify that in your view class
try to define the ___init__ method and put your logic in.
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...