Friday, November 29, 2013

Re: How to get the class name of a ContentType in django

Do you want to retrieve the class name of the model class associated with a content type?

Le jeudi 28 novembre 2013 12:04:34 UTC-5, Aamu Padi a écrit :
How do I get the class name in string of a ContentType? I tried it this way, but it didn't worked out:

class StreamItem(models.Model):
    user = models.ForeignKey(User)
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    pub_date = models.DateTimeField(default=datetime.now)

    content_object = generic.GenericForeignKey('content_type', 'object_id')
    content_class = content_type.__name__

    def __unicode__(self):
        return self.content_class

Any help will be much appreciated! Thank you.

--
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/46096fde-2754-49aa-af03-3829a5d1be45%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment