Django, query filtering from model method

前端 未结 5 535
醉酒成梦
醉酒成梦 2020-12-05 01:48

I have these models:

def Foo(Models.model):
    size = models.IntegerField()
    # other fields

    def is_active(self):
         if check_condition:
               


        
5条回答
  •  无人及你
    2020-12-05 01:56

    I had similar problem: I am using class-based view object_list and I had to filter by model's method. (storing the information in database wasn't an option because the property was based on time and I would have to create a cronjob and/or... no way)

    My answer is ineffective and I don't know how it's gonna scale on larger data; but, it works:

    q = Model.objects.filter(...)...
    # here is the trick
    q_ids = [o.id for o in q if o.method()]
    q = q.filter(id__in=q_ids)
    

提交回复
热议问题