Django gets ContentType in template

I have a page with a lot of objects with different types of content. I need to be able to evaluate these objects. Here is a class for it:

class Score(models.Model): user = models.ForeignKey(User) content_type = models.ForeignKey(ContentType) object_id = models.PositiveIntegerField() for_object = generic.GenericForeignKey('content_type', 'object_id') like = models.BooleanField(default=True) created_at = models.DateTimeField(auto_now_add=True, blank=True, null=True) comment = models.CharField(max_length=255, blank=True, null=True) objects = ChainerManager(ScoreQuerySet) def __unicode__(self): return u'Score for (%s, #%s) from user %s at %s' %\ (self.content_type, self.object_id, self.user.get_full_name(), self.created_at) class Meta: unique_together = (('user', 'content_type', 'object_id'),) 

And my template should look like this:

 ... {% for random_object in random_object_queryset %} <a href={% url like_object random_object.<content_type> random_object.id %}>{{ random_object.name }}</a> <a href={% url dislike_object random_object.<content_type> random_object.id %}>{{ random_object.name }}</a> {% endfor %} ... 

I can create a template tag to get it, or get the class name using this snippet: http://djangosnippets.org/snippets/294/ I can rewrite this snuppet to get the content_type_id for the object, but I'm a little afraid of a lot CT search in the database.

But is there a built-in method to get the CT object in the template?

View Code:

 def rate_object(request, classname, object_id, like=True): user = request.user Klass = ContentType.objects.get(model=classname).model_class() obj = get_object_or_404(Klass, user=user, pk=object_id) try: score = Score.objects.for_object(user, obj) score.like = like score.save() except Score.DoesNotExist: score = Score.objects.like(user, obj) if like else Score.objects.dislike(user, obj) return HttpResponse(obj) 
+8
python django django-templates django-contenttypes
source share
3 answers

To build the answer on @Colleen, I ended up using the template filter as follows:

 from django import template from django.contrib.contenttypes.models import ContentType register = template.Library() @register.filter def content_type(obj): if not obj: return False return ContentType.objects.get_for_model(obj) 

And used it in the template as follows:

 {% load helpers %} {% with instance|content_type as ctype %} <input type="hidden" name="content_type" value="{{ ctype.pk }}"> {% endwith %} 
+10
source share

I also had a situation where I needed the type of content in the template, and the only way I found was to get it through a special template tag.

In your situation, however, since you explicitly save the content_type as a foreign key, I would not worry about that. In the worst case, you can use prefetch_related() when you get evaluation objects in the view. I do not know if Django is enough to stop on the field, if you request the foreignkey.id file, this is the only one.

+2
source share

I prefer to do this with destination tags (new in Django 1.4):

 @register.assignment_tag def content_type(obj): if not obj: return False return ContentType.objects.get_for_model(obj) 

and used as

 {% content_type object as object_ct %} 
+1
source share

All Articles