Problem displaying related foreignkey data in Django Admin screen
To display foreignkey data in my admin list view, I created a callable:
def next_date(self):
EvDateObj = EventDate.objects.filter(event__id__exact=self.id)
.exclude(event_date__lt=datetime.date.today())开发者_JAVA技巧
.order_by('event_date')[:1]
return EvDateObj
This shows in the list view as:
[<EventDate: 25 September 2010>]
Which is the unicode string for the EventDate model (25 Sept 2010), with some django-generated object stuff around it: [< EventDate ______ >]
If I modify the callable return statement to try and just get the date itself:
return EvDateObj.event_date
or
return EvDateObj.event_date.strftime("%d %B %Y")
the admin list view simply shows:
(None)
Any thoughts? I am unsure how to proceed because I can get the desired object, but cannot access any of its properties without triggering the "(None)" result.
Have you tried:
EvDateObj = EventDate.objects.get(event__id__exact=self.id)
.exclude(event_date__lt=datetime.date.today())
.order_by('event_date')[:1]
objects.filter() always returns a QuerySet (similar to a Python List), even if there is only 1 result. EDateObj.objects.get() will return an object.
Alternatively you can do:
return EvDateObj[0]
I haven't tried it myself, so hope this works for you.
精彩评论