Private
Private

Reputation: 2694

How to correct wrong num_times in django-taggit-templatetags?

I have the following models

class Text(models.Model):
    text = models.CharField(max_length=10000, blank=True)
    tags = TaggableManager(blank=True)
    author = models.ForeignKey(User)
    date_created = models.DateTimeField(auto_now_add=True)
    last_modified = models.DateTimeField(auto_now=True) # changes on each edit
    public = models.BooleanField(default=1)

    def __unicode__(self):
        return self.text

class Note(models.Model):
    note = models.CharField(max_length=1000) 
    tags = TaggableManager(blank=True)
    text = models.ManyToManyField(Text)
    author = models.ForeignKey(User)
    date_created = models.DateTimeField(auto_now_add=True)
    last_modified = models.DateTimeField(auto_now=True) # changes on each edit
    public = models.BooleanField(default=1)

    def __unicode__(self):
        return u'%s' % (self.text.all())

I am using django-taggit and django-taggit-templatetags. When I make a view like this:

@staff_member_required #TODO disadvantage: redirects to admin page
def labels_all(request):
    return render_to_response('labels_all.html', locals(), context_instance=RequestContext(request))

with a template like

{% extends 'base.html' %}
{% load taggit_extras %}

{% block content %}

{% get_taglist as all_labels for 'notes' %}
<div class="tag-cloud">
<ul> 
{% for label in all_labels %}
<li>  
<a href="/labels/{{ label.slug }}"> 
    <font size={{label.weight|floatformat:0}}>
    {{ label|capfirst }} ({{ label.num_times }})
    </font>
</a>
</li> 
{% endfor %}
</ul> 
</div>    

Both models have a TaggableManager. I get the wrong num_times value when I make a taglist for either of the two models. The num_times I get are the number of times a specific tags occurs across the two above models (for instance, 71). I only want the number of times the tag occurs in the Note model (50).

I think the problem is in line 48 of this code: https://github.com/feuervogel/django-taggit-templatetags/blob/master/taggit_templatetags/templatetags/taggit_extras.py

It uses a call to taggit_taggeditem_items. I do not know where this comes from. In the database I have: taggit-tag (colums: id, name, slug) and taggit_taggeditem (id, tag_id, object_id, content_type_id). I do not know where it gets the _items bit, but I think it is from taggit's models.py BaseClass.

Could the problem be in the unicode method (which uses text in both models)?

In short, I want a tagcloud or taglist for a specific model. How can I use taggit and taggit-templatetags (or an alternative) to calculate tag frequencies (num_times) per model?

Thanks.

Upvotes: 0

Views: 295

Answers (1)

Akshar Raaj
Akshar Raaj

Reputation: 15231

Seems the name of your app is 'notes', and Text and Note are models inside this app.

If you want only tags used in model Text, you should use:

{% get_taglist as all_labels for 'notes.Text' %}

If you want only tags used in model Note, you should use:

{% get_taglist as all_labels for 'notes.Note' %}

Upvotes: 0

Related Questions