我有以下模型
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())我正在使用django-taggit和django-taggit-templatetag。当我这样做的时候:
@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))有一个模板
{% 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> 这两种型号都有一个TaggableManager。当我为这两个模型中的任何一个创建一个taglist时,我得到了错误的num_times值。我得到的num_times是上述两个模型(例如,71)中特定标记发生的次数。我只想知道标记在Note模型(50)中发生的次数。
我认为问题在代码的第48行:extras.py
它使用对taggit_taggeditem_items的调用。我不知道这是从哪里来的。在数据库中,我有:taggit(列: id、name、slug)和taggit_taggeditem (id、tag_id、object_id、content_type_id)。我不知道它是从哪里得到_items位的,但我认为它来自taggit的models.py BaseClass。
问题是否存在于unicode方法(在两个模型中都使用文本)?
简而言之,我想要一个特定模型的标签云或taglist。如何使用taggit和taggit-templatetag(或替代)计算每个型号的num_times()?
谢谢。
发布于 2013-12-20 19:23:38
你的应用程序的名字似乎是“notes”,Text和Note是这个应用程序中的模型。
如果只希望在模型文本中使用标记,则应使用:
{% get_taglist as all_labels for 'notes.Text' %}如果只希望在模型说明中使用标记,则应使用:
{% get_taglist as all_labels for 'notes.Note' %}https://stackoverflow.com/questions/17910226
复制相似问题