Jakob Damgaard Møller
2018-06-12 18:19:39 UTC
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 ?
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.
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.