How to get the class name of a ContentType in django

35 views
Skip to first unread message

Aamu Padi

unread,
Nov 28, 2013, 12:04:34 PM11/28/13
to django...@googlegroups.com
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.

Simon Charette

unread,
Nov 29, 2013, 10:14:19 PM11/29/13
to django...@googlegroups.com
Do you want to retrieve the class name of the model class associated with a content type?

Aamu Padi

unread,
Nov 30, 2013, 3:58:08 AM11/30/13
to django...@googlegroups.com
Yes. Could you kindly please show me how?


--
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...@googlegroups.com.
To post to this group, send email to django...@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.

Timothy W. Cook

unread,
Nov 30, 2013, 4:15:20 AM11/30/13
to django...@googlegroups.com
You can use type() on your instance(s) to find out their model class.

If you have an instance of the model:

stream = StreamItem.objects.create(user,ct,oid,pdate)

Then type(stream) should return <class 'StreamItem'>

Is that what you wanted?
> --
> 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...@googlegroups.com.
> To post to this group, send email to django...@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.



--
MLHIM VIP Signup: http://goo.gl/22B0U
============================================
Timothy Cook, MSc +55 21 94711995
MLHIM http://www.mlhim.org
Like Us on FB: https://www.facebook.com/mlhim2
Circle us on G+: http://goo.gl/44EV5
Google Scholar: http://goo.gl/MMZ1o
LinkedIn Profile:http://www.linkedin.com/in/timothywaynecook

Aamu Padi

unread,
Nov 30, 2013, 12:37:06 PM11/30/13
to django...@googlegroups.com
Yes!!! Can I use it as a string in template?


Timothy W. Cook

unread,
Nov 30, 2013, 5:11:52 PM11/30/13
to django...@googlegroups.com
On Sat, Nov 30, 2013 at 3:37 PM, Aamu Padi <aamu...@gmail.com> wrote:
> Yes!!! Can I use it as a string in template?
>


I haven't tested it but it seems you should be able to assign it to a
context variable or kwargs in a view and use it in a template.

Something like:

context['classname'] = type(myinstance)
or
kwargs['classname'] = type(myinstance)

HTH,
Tim
> https://groups.google.com/d/msgid/django-users/CAHSNPWsrPg8jC_hmSmgZfuO8KSucjCcb_V4h2PdePUxLwSK3oA%40mail.gmail.com.

Aamu Padi

unread,
Dec 1, 2013, 5:32:19 AM12/1/13
to django...@googlegroups.com
Ok, I will try it! Thank you so very much!!!


Timothy W. Cook

unread,
Dec 1, 2013, 6:40:36 AM12/1/13
to django...@googlegroups.com
Be sure to post the results back here for the archive so others may
find it in searches.
> https://groups.google.com/d/msgid/django-users/CAHSNPWuMJ5%2BP7ZxYH7R2OfAR4dDewyOyKMH8Z3o9CVTpgjhQ5A%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages