1

嗨,我是 Django 新手,正在编写我的第一个应用程序。我有一个基本模型:

from django.db import models
from django_extensions.db.fields import AutoSlugField

class Tag(models.Model):
    name = models.CharField(max_length=100)
    slug = AutoSlugField(populate_from='name', unique=True)

class Listings(models.Model):
    listing = models.CharField(max_length=50)
    description = models.CharField(max_length=500)
    email = models.EmailField(max_length=75)
    tag = models.ManyToManyField(Tag)
    pub_date = models.DateTimeField(auto_now=True)

def __unicode__(self):
        return self.listing    

我从终端添加了一些标签,但所有标签都显示为Tag Object. 如何获取要显示的标签名称?

另外,我如何让管理员添加标签,而不是每次都通过终端添加?

非常感谢!

4

1 回答 1

1

您在模型中缺少一个unicode属性Tag

class Tag(models.Model):
    name = models.CharField(max_length=100)
    slug = AutoSlugField(populate_from='name', unique=True)

    def __unicode__(self):
        return "%s"% self.name

此外,修复模型unicode块的缩进Listings

class Listings(models.Model):
    listing = models.CharField(max_length=50)
    description = models.CharField(max_length=500)
    email = models.EmailField(max_length=75)
    tag = models.ManyToManyField(Tag)
    pub_date = models.DateTimeField(auto_now=True)

    def __unicode__(self):
        return self.listing    
于 2013-06-26T16:59:45.473 回答