简体   繁体   English

Django模型外键过滤

[英]Django Model Foreign Key filtering

I need to filter some Models in Django and return them trough REST, but I have some difficulties. 我需要在Django中过滤一些模型并通过REST返回它们,但是我有一些困难。 I have 4 Models connected with Foreign key's like so: 我有4个与外键相连的模型,如下所示:

class Standort(models.Model):
    name = models.CharField(max_length=40)
    farbe = models.CharField(max_length=20, default="Black")

class Gruppe(models.Model):
    standort = models.ForeignKey(Standort)
    name = models.CharField(max_length=40)

class Person(models.Model):
    name = models.CharField(max_length=40)
    gruppe = models.ForeignKey(Gruppe, related_name='personen')

class Eintrag(models.Model):
    person = models.ForeignKey(Person, related_name='eintrage')
    typ = models.ForeignKey(Typ)
    datum = models.DateField()

and Iam serializing them like so: 和Iam像这样序列化它们:

class EintragSerializer(serializers.ModelSerializer):
    class Meta:
        model = Eintrag
        fields = ('datum', 'typ')


class PersonenSerializer(serializers.ModelSerializer):
    eintrage = EintragSerializer(read_only=True, many=True)

    class Meta(object):
        model = Person
        fields = ('id', 'name', 'eintrage')


class GruppenPersonenEintraegeSerializer(serializers.ModelSerializer):
    personen = PersonenSerializer(read_only=True, many=True)

    class Meta(object):
        model = Gruppe
        fields = ('id', 'name', 'personnel')

and my view looks like this: 我的看法如下所示:

class GruppenPersonenEintraege(APIView):
    def get(self, request, standort, jahr):
        gruppen = Gruppe.objects.filter(standort=standort)
        serializer = GruppenPersonenEintraegeSerializer(gruppen, many=True)
        return Response(serializer.data)

The result looks like this: 结果看起来像这样:

[
{
    "id": 2,
    "name": "2.Schicht",
    "personen": [
        {
            "id": 1,
            "name": "Rolf der Tester",
            "eintrage": [
                {
                    "datum": "2017-02-16",
                    "typ": 3
                },
                {
                    "datum": "2017-02-15",
                    "typ": 3
                },
                {
                    "datum": "2018-04-05",
                    "typ": 2
                }
            ]
        }
    ]
},
{
    "id": 3,
    "name": "Test",
    "personen": []
}
]

This is totally fine, my Problem is when i also want to filter the year of "eintrage.datum" by adding: .filter(standort=standort, personen__eintrage__datum__year=2017) after Gruppe.objects . 这完全没问题,我的问题是当我也想通过在"eintrage.datum"添加.filter(standort=standort, personen__eintrage__datum__year=2017)来过滤"eintrage.datum"Gruppe.objects Then the entry with "id": 2 is repeated 3 times and the one with "id": 3 isn't displayed at all. 然后,将"id": 2的条目重复3次,而"id": 3的条目将完全不显示。 how do i filter just the entry's of the second nested dict? 如何只过滤第二个嵌套词典的条目?

To avoid "id":2 repeated multi times, you can just add a list(set()) surround the filter queryset result, the django restful framework can also treat the list the same way as queryset. 为了避免多次重复出现“ id”:2,您可以在过滤器queryset结果周围添加一个list(set()),django restful框架也可以将列表与queryset一样对待。 Also notice that in django orm, the hash of a model instance is the prime_key of in db, so that's why the set can work on queryset. 还要注意,在Django orm中,模型实例的哈希值是db中的prime_key,因此这就是集合可以在queryset上使用的原因。

As for "id":3 not showing, I also have no ideas as you did, maybe double better check the db again. 至于“ id”:3没有显示,我也没有你的想法,也许最好再检查一次数据库。 A little bit more info will be more helpful. 多一点信息会更有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM