如何处理django多对多的mapper对象

时间:2013-04-15 18:25:49

标签: python django

我有模特:

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但它不起作用..

2 个答案:

答案 0 :(得分:1)

避免在模型字段中使用CamelCase。 Django Codigo Style - Model Field

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

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

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

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

答案 1 :(得分: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'
                )

告诉我它是否正常工作。