2010-03-04 205 views
0

我在我的一個類中有一個addedBy的字段。我已經建立了一個計數這一領域如下:用戶名不能正確顯示

user_count = Issues.objects.values('addedBy').annotate(count=Count('id')) 

然後在我的模板,我訪問變量

{% if user_count %} 
    <div class="menutitle"><strong>{% trans "Added By" %}</strong></div> 
     {% for item in user_count %} 
     <ul> 
      <li><a href="#">{{ item.addedBy }} ({{ item.count }})</a> </li> 
     </ul> 
     {% endfor %} 
{% endif %} 

但我得到的是用戶的ID不用戶名。

編輯:Issues型號

class Issues(models.Model): 
    ISSUE_CHOICES = (
    ('Mechanical', 'Mechanical'), 
    ('Dent', 'Dent'), 
    ('Interior', 'Interior'), 
    ('Body', 'Body'), 
    ('Part', 'Part'), 
    ('Other', 'Other'), 
    ) 
    ISSUE_STATUS_CHOICES = (
    ('In Progress', 'In Progress'), 
    ('Completed', 'Completed'), 
    ('Pending', 'Pending'), 
    ) 

    vehicle = models.ForeignKey(Vehicle) 
    description = models.CharField('Issue Description', max_length=30,) 
    type = models.CharField(max_length=10, default='Other', choices=ISSUE_CHOICES) 
    status = models.CharField(max_length=12, default='Pending', 
    choices=ISSUE_STATUS_CHOICES) 
    priority = models.IntegerField(default='8', editable=False) 
    addedBy = models.ForeignKey(User, related_name='added_by', editable=False) 
    assignedTo = models.ForeignKey(User, related_name='assigned_by', blank=True, 
    null=True) 
    dateTimeAdded = models.DateTimeField('Added On', default=datetime.today, 
    editable=False) 
    last_updated = models.DateTimeField(default=datetime.today, 
    editable=False) 
    def __unicode__(self):  
     return self.description 
    def save(self): 
    self.last_updated = datetime.now() 
    super(Issues, self).save() 
+0

你究竟在哪裏期待用戶名來自哪裏? – 2010-03-04 13:35:25

+0

向我們展示'models.py'中'Problems'模型的定義。 – 2010-03-04 14:09:27

回答

2

嘗試改變USER_COUNT分配:

user_count = Issues.objects.values('addedBy__username').annotate(count=Count('id')) 

和您的模板:

<li><a href="#">{{ item.addedBy__username }} ({{ item.count }})</a> </li> 

我有一個類似的問題之前,這解決了它。

+0

Thnx Luiz ...這個伎倆......對於遲到的回覆抱歉......從代碼中稍作休息。 – Stephen 2010-03-07 10:32:17