Django filter queryset on “tuples” of values for multiple columns

后端 未结 3 1563
孤独总比滥情好
孤独总比滥情好 2021-02-04 01:40

Say I have a model:

Class Person(models.Model):
    firstname = models.CharField()
    lastname = models.CharField()
    birthday = models.DateField()
    # etc.         


        
3条回答
  •  醉酒成梦
    2021-02-04 02:29

    Using python 3.5 version :

    import operator
    import functools
    
    query = functools.reduce(
        operator.or_, 
        (Q(firstname=fn, lastname=ln) for fn, ln in zip(first_list, last_list))
        )
    
    Person.objects.filter(query)
    

提交回复
热议问题