문제

I am trying to manage the admin console.

I have two models, a venue and an event: events can happen at venues and events have a timestamp.

In my admin console I have added the events to be listed in the venues admin by using inlines.

However I want to set up a case where, by default only events from timestamp.now() into the future are listed and all events before teimstamp.now() are not shown.

I am limited to Django 1.3 at the moment, but I believe Django 1.4 has a type of solution in SimpleListFilter.

I have read somewhere that DateFieldFilterSpec can be used, but documentation and examples are very lacking. (Can you point me out some references if you have them?)

In the python code below there is no connection between the import of DateFieldFilterSpec to the code shown below, but I have no idea where or how they are connected.

admin.py

from django.contrib.admin.filterspecs import DateFieldFilterSpec

class eventInline(admin.TabularInline):

    list_filter = ('now')

    model = event
    extra = 1

class VenueAdmin(admin.ModelAdmin):
   inlines = [eventInline,]

When I use the above code I just get all the events, there doesn't seem to be any filtering.

도움이 되었습니까?

해결책

How about modifying the queryset for that purpose (replace eventdate with the name of the field that holds the date of an event):

from datetime import datetime
from django.contrib import admin

class eventInline(admin.TabularInline):
    def queryset(self, request):
        qs = super(eventInline, self).queryset(request)
        return qs.filter(eventdate__gte=datetime.now)
    model = event
    extra = 1

class VenueAdmin(admin.ModelAdmin):
   inlines = [eventInline,]
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top