On Jul 17, 8:32 am, "Andre Meyer" <[EMAIL PROTECTED]> wrote:
> hi all
>
> imagine you have a first model class and a second model class with a
> ForeignKey referring to the first one. for example, a Post model (taken from
> the tutorial blog app) and a Comment model. of course, a post can have
> multiple comments, so the Post model gets an attribute 'comments' (specified
> using related_name).
>
> how can the comments be displayed in the list view of the posts using
> newforms-admin? i would like to indicate whether there are comments and how
> many, if any.

Create a method in your Post class like this:

def comment_stats(self):
   return self.comments.count()

Then add 'comment_stats' to the list_display of your Post's Admin
options.

>
> but when adding the 'comments' attribute to the list_display of PostOptions,
> syncdb throws an exception:
>
> *django.core.exceptions.ImproperlyConfigured: `PostOptions.list_display[13]`
> refers to `comments` that is neither a field, method or property of model
> `Post`.
> *
> which is not true! Post does get a 'comments' field by the definition of
> Comment.

>From the Post's perspective comments is a multi-valued field. However,
the Admin list_display of Post is intended to show one Post record per
row. So displaying post.comments where comments can have a dozen rows
per post doesn't make sense i.e. the Admin can't guess that you want
to show just the presence of comments as a boolean flag or that you
want to show the count of comments. The above technique of using a
custom method is how you can accomplish what you want here.

See:
http://code.djangoproject.com/wiki/NewformsHOWTO#Q:HowdoIaddanextracolumntothechangelistview

-Rajesh D
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to