how to access foreignkey field (related_name) in newforms-admin list view?
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. 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. inlined comments do appear in the detail view, though. what's wrong? thanks for your help André --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com 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 -~--~~~~--~~--~--~---
Re: how to access foreignkey field (related_name) in newforms-admin list view?
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 django-users@googlegroups.com 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 -~--~~~~--~~--~--~---
Re: how to access foreignkey field (related_name) in newforms-admin list view?
hi Rajesh thanks, that's a great link. still, it's not exactly clear to me why the admin does not work the same as other templates. in my own template, it is no problem to refer to comments of a post using, e.g. {% for comment in post.comments.all %} [...] {% endfor %} or {{ post.comments.all|length }} looks inconsistent to me. thanks & cheers André On Thu, Jul 17, 2008 at 4:14 PM, Rajesh Dhawan <[EMAIL PROTECTED]> wrote: > > > > 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 django-users@googlegroups.com 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 -~--~~~~--~~--~--~---
Re: how to access foreignkey field (related_name) in newforms-admin list view?
Hi, > still, it's not exactly clear to me why the admin does not work the same as > other templates. The Admin is not a template. So, I don't know what you mean by that. > > in my own template, it is no problem to refer to comments of a post using, > e.g. > > {% for comment in post.comments.all %} > [...] > {% endfor %} > > or > > {{ post.comments.all|length }} > > looks inconsistent to me. The whole point is that in your own template, you decide whether you want to display the count of comments or a list of comments or the names of the commenters, and so on. How would the Admin know what you want if you were allowed to put "comments" in the list_display? One user may want the count, another may want something else. That's why the Admin supports calling methods in your model class. And through such methods you are able to display your comments field exactly the way you want. -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 django-users@googlegroups.com 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 -~--~~~~--~~--~--~---
Re: how to access foreignkey field (related_name) in newforms-admin list view?
hi Rajesh On Thu, Jul 17, 2008 at 6:55 PM, Rajesh Dhawan <[EMAIL PROTECTED]> wrote: > > Hi, > > > still, it's not exactly clear to me why the admin does not work the same > as > > other templates. > > The Admin is not a template. So, I don't know what you mean by that. admin is not using templates? well, i did not check the implementation. The whole point is that in your own template, you decide whether you > want to display the count of comments or a list of comments or the > names of the commenters, and so on. How would the Admin know what you > want if you were allowed to put "comments" in the list_display? One > user may want the count, another may want something else. That's why > the Admin supports calling methods in your model class. And through > such methods you are able to display your comments field exactly the > way you want. agree, though it would make sense to me to return the RelatedManager instance, just like in the templates. anyway, problem solved thanks a lot André --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com 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 -~--~~~~--~~--~--~---
Re: how to access foreignkey field (related_name) in newforms-admin list view?
> > > still, it's not exactly clear to me why the admin does not work the same > > as > > > other templates. > > > The Admin is not a template. So, I don't know what you mean by that. > > admin is not using templates? well, i did not check the implementation. Sorry, if I wasn't clear. The admin does use templates but it's much more than that (that's why I said that the Admin is not a template). Moreover, the Admin change-list and all its other templates have to be dynamic because they don't know beforehand what fields each model is going to have and which of those are going to be displayed via list_display. So, as I explained above, there's no way for the admin to guess what you want it to do with related many-valued fields like Post.comments in your case. So, it chokes when you try to do such a thing. > > The whole point is that in your own template, you decide whether you > > > want to display the count of comments or a list of comments or the > > names of the commenters, and so on. How would the Admin know what you > > want if you were allowed to put "comments" in the list_display? One > > user may want the count, another may want something else. That's why > > the Admin supports calling methods in your model class. And through > > such methods you are able to display your comments field exactly the > > way you want. > > agree, though it would make sense to me to return the RelatedManager > instance, just like in the templates. What would that give you? In the change list you would just see the class name of the RelatedManager. Is that what you mean? Remember that in your own templates, you don't just say: {{ post.comments }} Instead you either loop over comments or do a length filter on them, etc. In other words, you are actively making the decision on what you want to do with that related manager instance. Also, there's one other important difference: when you loop over post.comments in your template, you are causing a DB query that brings in the comments of that single post instance. If an Admin were to do that, it would have to issue an extra query for each row it is going to display in the change-list screen. All this is documented in the second bullet here: http://www.djangoproject.com/documentation/model-api/#list-display -Rajesh --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com 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 -~--~~~~--~~--~--~---
Re: how to access foreignkey field (related_name) in newforms-admin list view?
hi Rajesh so, implemented it as you told and it works very well. thanks for your patience and all the explanations. cheers André On Thu, Jul 17, 2008 at 7:22 PM, Rajesh Dhawan <[EMAIL PROTECTED]> wrote: > > > > > > still, it's not exactly clear to me why the admin does not work the > same > > > as > > > > other templates. > > > > > The Admin is not a template. So, I don't know what you mean by that. > > > > admin is not using templates? well, i did not check the implementation. > > Sorry, if I wasn't clear. The admin does use templates but it's much > more than that (that's why I said that the Admin is not a template). > Moreover, the Admin change-list and all its other templates have to be > dynamic because they don't know beforehand what fields each model is > going to have and which of those are going to be displayed via > list_display. > > So, as I explained above, there's no way for the admin to guess what > you want it to do with related many-valued fields like Post.comments > in your case. So, it chokes when you try to do such a thing. > > > > > The whole point is that in your own template, you decide whether you > > > > > want to display the count of comments or a list of comments or the > > > names of the commenters, and so on. How would the Admin know what you > > > want if you were allowed to put "comments" in the list_display? One > > > user may want the count, another may want something else. That's why > > > the Admin supports calling methods in your model class. And through > > > such methods you are able to display your comments field exactly the > > > way you want. > > > > agree, though it would make sense to me to return the RelatedManager > > instance, just like in the templates. > > What would that give you? In the change list you would just see the > class name of the RelatedManager. Is that what you mean? > > Remember that in your own templates, you don't just say: > > {{ post.comments }} > > Instead you either loop over comments or do a length filter on them, > etc. In other words, you are actively making the decision on what you > want to do with that related manager instance. > > Also, there's one other important difference: when you loop over > post.comments in your template, you are causing a DB query that brings > in the comments of that single post instance. If an Admin were to do > that, it would have to issue an extra query for each row it is going > to display in the change-list screen. > > All this is documented in the second bullet here: > > http://www.djangoproject.com/documentation/model-api/#list-display > > -Rajesh > > > --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com 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 -~--~~~~--~~--~--~---