Discussion:
Get filtered data from url
Agu Aguilar
2018-10-11 17:48:14 UTC
Permalink
Hi there!
I have a model:

from django.contrib.sites.models import Site

class Person(models.Model):
site = ForeignKey(Site, on_delete=models.CASCADE)


I have two serializers:
from .models import Person
from django.contrib.sites.models import Site

class PersonSerializer(serializers.ModelSerializer):
class Meta:
fields = '__all__'
model = Person

class SiteSerializer(serializers.ModelSerializer):
class Meta:
fields = '__all__'
model = Site


I have a model view set
from .serializers import PersonSerializer

class PersonViewSet(viewset.ModelViewSet):
pass



How I would do to get all person that have a associate site from this url
"/api/site/{site_id}/person/"?
How would my router into urls.py look?

Thanks!
--
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.
Clara Daia
2018-10-26 21:28:10 UTC
Permalink
I used one of the suggested third party libraries
<https://www.django-rest-framework.org/community/third-party-packages/>:
specifically drf-nested-routers
<https://github.com/alanjds/drf-nested-routers>. The 'urls.py' code looks
pretty much the same as the example.
Post by Agu Aguilar
Hi there!
from django.contrib.sites.models import Site
site = ForeignKey(Site, on_delete=models.CASCADE)
from .models import Person
from django.contrib.sites.models import Site
fields = '__all__'
model = Person
fields = '__all__'
model = Site
I have a model view set
from .serializers import PersonSerializer
pass
How I would do to get all person that have a associate site from this url
"/api/site/{site_id}/person/"?
How would my router into urls.py look?
Thanks!
--
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...