开发者

Username not being displayed correctly

I have a field for addedBy in one of my classes. I've set up a count for this field as below:

user_count = Issues.objects.values('addedBy').anno开发者_如何转开发tate(count=Count('id'))

Then in my template I'm accessing the variable as

{% 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 %}

But all I get is the ID for the user not the username as intended.

EDIT: Issues Model

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()


Try changing the user_count assignment to:

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

and on your template:

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

I had a similar issue before and this solved it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜