On Thursday, November 21, 2013 6:22:29 AM UTC-5, Xiao Jia wrote:
I am following the tutorial of Django 1.6, which includes a model
Pollthat has a derived attributewas_published_(a method ofrecently class Poll). The model was originally defined as follows.# polls/models.py (excerpted) class Poll(models.Model): # ... def was_published_recently(self): return self.pub_date >= timezone.now() - datetime.timedelta(days=1)And the admin interface for this app:
# polls/admin.py (excerpted) class PollAdmin(admin.ModelAdmin): # ... list_display = ('question', 'pub_date', 'was_published_recently') admin.site.register(Poll, PollAdmin)Now we want to improve the display and sorting functionality of
was_published_recently.In the tutorial, file
polls/models.pyis updated:class Poll(models.Model): # ... was_published_recently.admin_order_field = 'pub_date' was_published_recently.boolean = True was_published_recently.short_description = 'Published recently?'However, I think this may not be good enough in practice, because what we are specifying is completely about the admin user interface, not the model per se. So I instead updated
polls/admin.py:class PollAdmin(admin.ModelAdmin): Poll.was_published_recently.admin_order_field = 'pub_date' Poll.was_published_recently.boolean = True Poll.was_published_recently.short_description = 'Published recently?' # ...After this modification, the app works as expected as well (multiple polls work well also). Since I am new to Python, I investigated a bit further by printing
was_published_in bothrecently PollandPollAdmin:class Poll(models.Model): # ... print("in class Poll", was_published_recently) class PollAdmin(admin.ModelAdmin): print("in class PollAdmin", Poll.was_published_recently) # ...And the output is
in class Poll <function Poll.was_published_recently at 0x10fc92050> in class PollAdmin <function Poll.was_published_recently at 0x10fc92050>So apparently
was_published_inrecently class Pollis the same asPoll.published_recentlyaccessed in class PollAdmin.My question: Should I specify the
admin_order_fieldstuffs inadmin.pyormodels.py? If specified inadmin.pyinstead ofmodels.py, are there any drawbacks?
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/b5e0aef2-ce4d-43c7-b591-f919f06a6849%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
No comments:
Post a Comment