If your model have two dates, start and end of something, you may want to have filter which allow you to show objects which last on specific day.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | import datetime
from django import admin
from django.db.models import Min, Max
class AvaliabilityAtDateListFilter(admin.SimpleListFilter):
title = 'avaliability at date'
parameter_name = 'date'
def lookups(self, request, model_admin):
stats = model_admin.model.objects.aggregate(Min('start'), Max('end'))
start = stats.get('start__min')
end = stats.get('end__max')
days = list()
while (start <= end):
wynik.append(start)
start = start + datetime.timedelta(days = 1)
return [(d, d) for d in days]
def queryset(self, request, queryset):
if self.value():
return queryset.filter(
start__lte=self.value(), end__gte=self.value())
else:
return queryset
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 11 months ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 1 week ago
- Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
- Help text hyperlinks by sa2812 1 year, 7 months ago
Comments
Please login first before commenting.