Login

AvaliabilityAtDateListFilter

Author:
m_aciek
Posted:
August 27, 2016
Language:
Python
Version:
1.10
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

  1. Template tag - list punctuation for a list of items by shapiromatron 10 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 6 months ago

Comments

Please login first before commenting.