0

我有模型:

class tags(models.Model):
    """ This is the tag model """
    tag = models.CharField(max_length=15)               # Tag name
    tagDescription = models.TextField()                 # Tag Description
    tagSlug = models.CharField(max_length=400)          # Extra info can be added to the existing tag using this field
    createdAt = models.DateTimeField(auto_now_add=True)
    updatedAt = models.DateTimeField(auto_now=True)
    def __unicode__(self):   
        return unicode(self.tag)


class stores(models.Model):
    """ This is the store model """
    storeName = models.CharField(max_length=15)                                          # Store Name
    storeDescription = models.TextField()                                                # Store Description
    storeURL = models.URLField()                                                         # Store URL
    storePopularityNumber = models.IntegerField(max_length=1)                            # Store Popularity Number
    storeImage = models.ImageField(upload_to=storeImageDir)                              # Store Image 
    storeSlug = models.CharField(max_length=400)                                                       # This is the text you see in the URL
    createdAt = models.DateTimeField(auto_now_add=True)                                                  # Time at which store is created
    updatedAt = models.DateTimeField(auto_now=True)                                                   # Time at which store is updated
    storeTags = models.ManyToManyField(tags)                                             # All the tags associated with the store
    def __unicode__(self):
        return unicode(self.storeName)

    def StoreTags(self):
        return unicode(self.storeTags.all())

它在 StoreTags 下显示 [] 这是 storesAdmin 类:

class storesAdmin(admin.ModelAdmin):
    list_display = ('storeName','storeDescription','storeURL',
                    'storePopularityNumber','storeImage',
                    'storeSlug','createdAt','createdAt','StoreTags'
                    )

为什么它会这样显示我什至试图将其转换为 unicode 但它不起作用..

4

2 回答 2

1

避免在模型字段中使用 CamelCase。Django Codigo 风格 - 模型字段

“字段名称应全部小写,使用下划线而不是驼峰式。”

避免在函数和方法中使用 CamelCase。

“使用下划线,而不是 camelCase,用于变量、函数和方法名称(即 poll.get_unique_voters(),而不是 poll.getUniqueVoters)。”

尝试为 storetags 方法选择另一个名称。也许它与 storetags 字段 name.django 哈希对象冲突

于 2013-04-15T21:22:01.343 回答
0

尝试使用代码:

models
class Tags(models.Model):
    #...
    def __unicode__(self):   
        return '%s' % self.tag

class Stores(models.Model):
    #...
    def __unicode__(self):
        return '%s' % self.storeTags.tag

admin, list_display is not supported to ManyToMany, i'm remove storetags
class storesAdmin(admin.ModelAdmin):
    list_display = ('storename','storedescription','storeurl',
                'storepopularitynumber','storeimage',
                'storeslug','createdat','createdat'
                )

告诉我它是否正常工作。

于 2013-04-15T22:28:33.120 回答