Thursday, November 28, 2013

How to get the class name of a ContentType in django

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/CAHSNPWtmzHebDbd%3DG01jtMzNaZ%3DF5HKbN0LWgzpHs6ghKuQeYg%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment