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 2 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 2 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 9 months, 2 weeks ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 10 months, 1 week ago
  5. Help text hyperlinks by sa2812 11 months ago

Comments

Please login first before commenting.