On Jun 7, 2016 1:35 PM, "'David Turner' via Django users" <django-users@googlegroups.com> wrote:
>
> Being new to Django I have a problem which I am not able to resolve.
>
> I have a model which has the following fields:
> expiry_date = models.DateField(null=True, blank=True,)
> has_expired = models.BooleanField(verbose_name='Has Expired',
> default=True, )
>
> I need to keep expired items in the database for reporting purposes.
>
> My question is how do I show only the non-expired items in my views.?
>
> Would the solution be via a model manager?
>
>Probably, yes.
If you only need to filter out expired items in a single view, you may just want to override/append the query set used in that single view with filter(has_expired=False).
If you plan on needing this query filtering in multiple locations, then you'll want to create a custom model manager method that includes the desired filter.
On a side note, you may not need the has_expired fields at all if your business logic dictates that any instance with an expiry_date that has already passed should be considered expired. It would mitigate the need for the extra logic to keep the has_expired field in sync with the expiry_date. Otherwise you may end up with objects with an expiry_date in the future but showing expired, and vice versa. I try to avoid those types of field dependencies, and DB normalization purists will likely complain about it.
-James
--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/aSj3jGX2CLk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2Be%2BciVY43_D-9YF9PK_intnsEkd%2BRDJoSVr6h-O79%2BzP%2B3Gcg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CALwQ%2B-swvJ7-Crs7%3DZLpJF5YcQ%2BgHAfpL4KQT9EVNkcZ%3D-nEBQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment