0

我已经编写了以下模型,但可能有问题,因为 Django 管理站点中返回的不是标签名称,而是标签对象:

from os import path
from django.db import models
from django.contrib import admin
from django.core.files.storage import FileSystemStorage

projectDirPath = path.dirname(path.dirname(__file__)) 
storeImageDir = FileSystemStorage(location=projectDirPath + '/couponRestApiApp/static')

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)

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 getStoreTags(self):
        p = self.storeTags.objects.all()
        return p.tag

class coupons(models.Model):
    """ This is the coupon model """
    couponValue = models.CharField(max_length=4)                              # Coupon value in RS.
    couponDescription = models.TextField()                                    # Coupon Description
    couponURL = models.URLField()                                             # Coupon click URL
    couponStore = models.ForeignKey(stores)                                   # Key of coupon to store
    tagName = models.ForeignKey(tags,on_delete=models.PROTECT)                # Tag names associated to coupon
    success = models.TextField()                                              # Count of the number of times people have made it work
    failures =  models.TextField()                                            # Count of the number of times this has failed
    lastTested = models.DateTimeField(auto_now=True)                          # When was the coupon last tested
    createdAt = models.DateTimeField(auto_now_add=True)
    updatedAt = models.DateTimeField(auto_now=True)

class app(models.Model):
    """ This is the application model which is using the API """
    appName = models.CharField(max_length=20)      # Application name
    appDomain = models.CharField(max_length=20)    # Application description
    appKey =  models.TextField()                   # Application Key 
    createdAt = models.DateTimeField(auto_now_add=True)             # Time at which Application is added is created
    updatedAt = models.DateTimeField(auto_now=True)             # Time at which Application details are updated

class subscriptions(models.Model):    
    """ These are the emails that are subscribing """  
    app =  models.CharField(max_length=20)                                # The application where the email came from
    store = models.CharField(max_length=20)                               # The optional store on which the email wants an update
    tag =   models.CharField(max_length=20)                               # The optional tag on which the email wants an update
    emailID = models.EmailField()                                         # EmailID of the registered user
    active =  models.BooleanField(default=True)                           # They may have unsubscribed
    createdAt = models.DateTimeField(auto_now_add=True)                                    # Time at user subscribed to the alerts
    updatedAt = models.DateTimeField(auto_now=True)                                    # Time at which user updated its subscription 

class tagsAdmin(admin.ModelAdmin):
    list_display = ('tag', 'tagDescription', 'tagSlug')

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

class couponsAdmin(admin.ModelAdmin):
    list_display = ('couponValue','couponDescription','couponValue',
                    'couponURL', 'couponStore','tagName','success',
                    'failures','createdAt','updatedAt'
                    )

class appsAdmin(admin.ModelAdmin):
    list_display = ('appName','appDomain','appKey',
                    'createdAt','updatedAt'                    
                    )


class subcriptionsAdmin(admin.ModelAdmin):
    list_display = ('app','store','tag','emailID',
                    'active','createdAt','updatedAt'
                    )

admin.site.register(tags,tagsAdmin)
admin.site.register(stores,storesAdmin)
admin.site.register(coupons,couponsAdmin)
admin.site.register(app,appsAdmin)
admin.site.register(subscriptions,subcriptionsAdmin)

但是,当我使用 Django 管理站点在优惠券类别下保存数据而不是返回标签标签对象的名称时,商店也会发生同样的情况......我做错了什么......请帮帮我。我已经注册了所有条目,但仍然无法正常工作。

4

1 回答 1

2

您必须定义 unicode 才能正确显示:

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)          
    createdAt = models.DateTimeField(auto_now_add=True)
    updatedAt = models.DateTimeField(auto_now=True)
    def __unicode__(self):  # <-- you have to "tell" Django which field to display in admin 
        return self.tag
于 2013-04-15T17:39:16.913 回答