Login

AvaliabilityAtDateListFilter

Author:
m_aciek
Posted:
August 27, 2016
Language:
Python
Version:
1.10
Tags:
filter admin changelist list_filter
Score:
1 (after 1 ratings)

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

Comments

Please login first before commenting.