Saturday, November 30, 2013

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

On Sat, Nov 30, 2013 at 3:37 PM, Aamu Padi <aamupadi@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

>
> On Sat, Nov 30, 2013 at 2:45 PM, Timothy W. Cook <tim@mlhim.org> wrote:
>>
>> 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?
>>
>>
>>
>>
>>
>> On Sat, Nov 30, 2013 at 1:14 AM, Simon Charette <charette.s@gmail.com>
>> wrote:
>> > 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.
>>
>>
>>
>> --
>> 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
>>
>> --
>> 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/CA%2B%3DOU3XfRZy7XmTwof5QkNB3oe%2BhF0PVJO_XW8SWi9QdxqnFRg%40mail.gmail.com.
>>
>> For more options, visit https://groups.google.com/groups/opt_out.
>
>
> --
> 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/CAHSNPWsrPg8jC_hmSmgZfuO8KSucjCcb_V4h2PdePUxLwSK3oA%40mail.gmail.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

--
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/CA%2B%3DOU3WpkMK95w7xsHXmeHtYubynaqKHQHecLzzGnY9mSfs_Sw%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment