django rest框架过滤相关模型

时间:2016-08-22 13:20:35

标签: python django django-rest-framework

我有一个模范国家和某个国家某一年度假的人模型。我希望有一个国家的Api,我只能过滤某些人在某一年度假的国家。

Models.py

from django.db import models

class Country(models.Model):
  id = models.CharField(max_length=50,primary_key=True)
  country = models.CharField(max_length=255,null=True)

class PersonYear(models.Model):
  id = models.IntegerField(primary_key=True)
  person = models.CharField(max_length=255,null=True)
  year = models.IntegerField(null=True)
  country = models.ForeignKey(Country, related_name='personyears')

模特国家的内容

id|country
1|France
2|Italy
3|Spain

模特PersonYear的内容

id|person|year|country_id
1|John|2014|1
2|John|2015|1
3|Mary|2014|1

serializers.py

from apiapp.models import PersonYear,Country
from rest_framework import serializers

class PersonyearSerializer(serializers.HyperlinkedModelSerializer):

   class Meta:
      model = PersonYear
      fields = ('id','person','year')

class CountrySerializer(serializers.HyperlinkedModelSerializer):
   personyears = PersonyearSerializer(many=True)

   class Meta:
      model = Country
      fields = ('id','country','personyears')

Views.py

import rest_framework_filters as filters

class PersonyearFilter(filters.FilterSet):
   id = filters.AllLookupsFilter(name='id')
   person = filters.AllLookupsFilter(name='person')
   year = filters.AllLookupsFilter(name='year')

   class Meta:
      model = PersonYear

class PersonyearViewSet(viewsets.ModelViewSet):
   queryset = PersonYear.objects.all()
   serializer_class = PersonyearSerializer
   filter_backends = (filters.backends.DjangoFilterBackend,)
   filter_fields =  ['id','person','year']
   filter_class = PersonyearFilter

class CountryFilter(filters.FilterSet):
   id = filters.AllLookupsFilter(name='id')
   nm = filters.AllLookupsFilter(name='country')
   personyears = filters.RelatedFilter(PersonyearFilter,name='personyears') 

   class Meta:
      model = Country 

class CountryViewSet(viewsets.ModelViewSet):
   queryset = Country.objects.all()
   serializer_class = CountrySerializer
   filter_backends = (filters.backends.DjangoFilterBackend,)
   filter_fields = ['id','country','personyears']
   filter_class = CountryFilter

我想要选择约翰在2014年度假的所有国家:

http://localhost:8000/country/?personyears__person=John&personyears__year=2014

我希望得到一条记录:

{"id": "1", "country": "France",
   "personyears": [
    {   "id": 1,
        "person": "John"
        "year": 2014
    }
]
}

但我把这个记录重复了4次。你能解释一下我做错了什么以及如何得到我想要的东西。

Update1:​​

我不想在2014年为John提供特殊的解决方案。我想在AnyYear中为Anyperson的所有实例提供解决方案。例如,我还希望以下过滤器给出一个结果: http://localhost:8001/api/country/?personyears__person=Mary&personyears__year=2014

UPDATE2:

我尝试更换:

 queryset = Country.objects.all()

由:

 queryset = Country.objects.all().distinct()

它有助于获得预期的(1记录)结果:

本地主机:8000 /国家/ personyears__person =约翰&安培; personyears__year = 2014

但是我现在为person ='Mary'/ year ='2015'

获得意外/不需要的结果

本地主机:8000 /国家/ personyears__person =玛丽&安培; personyears__year = 2015

我预计没有结果(玛丽在2015年没有去任何国家度假)。但我得到了

  {"id": "1","country": "France",
   "personyears": [
            {
                "id": 1,
                "person": "John",
                "year": 2014
            },
            {
                "id": 2,
                "person": "John",
                "year": 2015
            },
            {
                "id": 3,
                "person": "Mary",
                "year": 2014
            }
   ]
   }

1 个答案:

答案 0 :(得分:1)

您的queryset应该是:

Country.objects.filter(personyears__year='your_year', person='your_person').values_list('country', flat=True)

这将返回所有国家/地区的列表。

注意:这将根据不同用户可能相同的人名进行过滤。应该还有一个模型Personforeign key应该映射到PersonYear