سؤال

I have this:

class A(models.Model):
    name = models.CharField()
    tags = models.ManyToManyField(Tags, through='Tags')

class Tags(models.Model)
    a = models.ForeignKey(A)
    tag = models.ForeignKey(Tag)
    assign_date = models.DateTimeField()

class Rates(models.Model):
    a = models.ForeignKey(A)
    rate = models.PositiveSmallIntegerField()

I need to get all Rates bigger then 2 and filter by tags assigned later then given date. The date filter is easy but I don't know how to use django orm for this query

I need something like this:

rates = rates.objects.filter(rate_gte=2, a__tags__assign_date__gte=date_object)

On the other hand, the sql query is very easy :

select * from rates
inner join tags on tags.a_id = rates.a_id
where tags.assign_date > now() - '2 weeks' and rates.rate > 2
هل كانت مفيدة؟

المحلول

What you have is almost correct. You want "rate__gte" notice the second underscore.

There is an example of usage here: https://docs.djangoproject.com/en/dev/topics/db/queries/#filtered-querysets-are-unique

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top