Vlad T.
Vlad T.

Reputation: 2608

How to get values('content_object')?

I use standard django.contrb.comments app...

django/contrib/comments/models.py

class BaseCommentAbstractModel(models.Model):
    """
    An abstract base class that any custom comment models probably should
    subclass.
    """

    # Content-object field
    content_type = models.ForeignKey(ContentType,
            verbose_name=_('content type'),
            related_name="content_type_set_for_contrib_%(class)s")
    object_pk = models.TextField(_('object ID'))
    content_object = generic.GenericForeignKey(ct_field="content_type", fk_field="object_pk")
    ...

and need to get list of comments with related objects tied through generic relations:

views.py

from django.contrib.comments import Comment

comment_list = list(Comment.objects.values_list('id', 'content_object')

but it throws an error:

FieldError: Cannot resolve keyword 'content_object' into field. Choices are: comment, content_type, flags, id, ip_address, is_public, is_removed, object_pk, site, submit_date, user, user_email, user_name, user_url

Is there a way to do this?

Upvotes: 3

Views: 911

Answers (1)

falsetru
falsetru

Reputation: 369274

content_object is GenericForeignKey field. You should speficy object_pk (fk_field) instead:

Comment.objects.values_list('id', 'object_pk')

UPDATE

To get comment ids, url for objects:

[(pk, urlresolvers.reverse('comments-url-redirect', args=(ct, o)))
 for pk, o, ct in Comment.objects.values_list('id', 'object_pk', 'content_type')]

Upvotes: 2

Related Questions