2014-06-16 199 views
0

我試圖擴展已有的模型,所以我在其中添加了「縮略圖」。不幸的是,與此相關的功能是不承認和Django控制檯給我:模型中的功能無法識別

thumbnail = models.ImageField(upload_to=_get_upload_image, blank=True, null=True) 
NameError: name '_get_upload_image' is not defined 

有人可以幫我解決這個問題嗎?

的Django 1.6.5 models.py(短版)

class Feed(models.Model): 
    link = models.CharField(blank=True, max_length=450) 
    url = models.CharField(blank=True, max_length=450) 
    title = models.CharField(blank=True, null=True, max_length=250) 
    category = models.ForeignKey(Category, blank=True, null=True) 
    user = models.ForeignKey(User, blank=True, null=True) 
    last_update = models.DateField(blank=True, null=True, editable=False) 
    country = models.ForeignKey(Country, blank=True, null=True) 
    thumbnail = models.ImageField(upload_to=_get_upload_image, blank=True, null=True) 

    class Meta: 
     unique_together = (
      ("url", "user"), 
     ) 

    def _get_upload_image(instance, filename): 
     return "images/%s_%S" % (str(time()).replace('.','_'), filename) 
+1

變化_get_upload_image'的'接入從一個類的方法來一個輔助方法(de-indent一級),它將工作 – karthikr

+0

我已經在這個類中有更多的功能,這是應用django-feedme,我想擴展它。 – Robert

回答

0

我解決了這個通過將功能上類的頂部

class Feed(models.Model): 

    def _get_upload_image(instance, filename): 
     return "images/%s_%S" % (str(time()).replace('.','_'), filename) 

    link = models.CharField(blank=True, max_length=450) 
    url = models.CharField(blank=True, max_length=450) 
    title = models.CharField(blank=True, null=True, max_length=250) 
    category = models.ForeignKey(Category, blank=True, null=True) 
    user = models.ForeignKey(User, blank=True, null=True) 
    last_update = models.DateField(blank=True, null=True, editable=False) 
    country = models.ForeignKey(Country, blank=True, null=True) 
    thumbnail = models.ImageField(upload_to=_get_upload_image, blank=True, null=True)