Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django filter query on with property fields automatically calculated

Tags:

There is Django Order model with property fields automatically calucated. How to do a filter query.

class Order(models.Model):

    @property
    def expire(self):
        return self.created + datetime.timedelta(days=self.days_left())

    @property
    def days_left(self):
        return self.recurrence_period  * self._recurrence_unit_days[self.recurrence_unit]

Calculation done to get 1,3,7 datetime days from today

settings.SUBSCRIPTION_EXPIRATION_REMIND = [1, 3, 7]

days = map(lambda x: datetime.date.today() + datetime.timedelta(days=x), settings.SUBSCRIPTION_EXPIRATION_REMIND)

[datetime.date(2015, 7, 28),
 datetime.date(2015, 7, 30),
 datetime.date(2015, 8, 3)]

How to filter by ORM query

Order.objects.filter(expire__in=days)

Django is throwing error.

FieldError: Cannot resolve keyword 'expire' into field.
like image 247
bobsr Avatar asked Jul 27 '15 16:07

bobsr


People also ask

Can you filter by property Django?

Nope. Django filters operate at the database level, generating SQL. To filter based on Python properties, you have to load the object into Python to evaluate the property--and at that point, you've already done all the work to load it.

How can I filter a Django query with a list of values?

Django has special __in operator that we can use with Django filter() method. This will return query set from the model “Contact” after filtering a Django query with a list of values.

How do you use get and filter together in Django?

This exception is also an attribute of the model class. Returns a new QuerySet containing objects that match the given lookup parameters. Basically use get() when you want to get a single unique object, and filter() when you want to get all objects that match your lookup parameters. Show activity on this post.

What is the purpose of filter () method in Django?

The filter() method is used to filter you search, and allows you to return only the rows that matches the search term.


2 Answers

No, you can't perform lookup based on model methods or properties. Django ORM does not allow that.

Queries are compiled to SQL to be sent and processed at the database level whereas properties are Python code and the database knows nothing about them. That's the reason why the Django filter only allows us to use database fields.

Can do this:

Order.objects.filter(created=..) # valid as 'created' is a model field

Cannot do this:

Order.objects.filter(expires=..) # INVALID as 'expires' is a model property

You can instead use list comprehensions to get the desired result.

[obj for obj in Order.objects.all() if obj.expire in days]

The above will give me the list of Order objects having expire value in the days list.

like image 175
Rahul Gupta Avatar answered Sep 19 '22 11:09

Rahul Gupta


I dont think you can use a property in the field lookups as the doc says The field specified in a lookup has to be the name of a model field https://docs.djangoproject.com/en/1.8/topics/db/queries/#field-lookups

like image 31
Shaikhul Avatar answered Sep 19 '22 11:09

Shaikhul