2010-06-07 167 views
2

我不知道我的錯誤在哪裏,但Django 1.2.1是給這個錯誤:'NoneType'對象沒有屬性'天'當我嘗試保存表單從管理員區**更新**'NoneType'對象沒有屬性'天'

models.py

from django.db import models 
from django.contrib.auth.models import User 


class Editorial(models.Model): 

    titulo = models.CharField(max_length=250,help_text='Titulo del editorial') 
    editorial = models.TextField(help_text='Editorial') 
    slug = models.SlugField(unique_for_date='pub_date') 
    autor = models.ForeignKey(User) 
    pub_date = models.DateTimeField(auto_now_add=True) 
    activa = models.BooleanField(verbose_name="Activa") 
    enable_comments = models.BooleanField(verbose_name="Aceptar Comentarios",default=False) 

    editorial_html = models.TextField(editable=False,blank=True) 

    def __unicode__(self): 
     return unicode(self.titulo) 

    def get_absolute_url(self): 
     return "/editorial/%s/%s/" % (self.pub_date.strftime("%Y/%b/%d").lower(), self.slug) 

    class Meta: 
     ordering=['-pub_date'] 
     verbose_name_plural ='Editoriales' 

    def save(self,force_insert=False, force_update=False): 
     from markdown import markdown 
     if self.editorial: 
      self.editorial_html = markdown(self.editorial) 
     super(Editorial,self).save(force_insert,force_update) 

我不知道爲什麼這個錯誤,

已完成ERROR:

Traceback: 
File "C:\wamp\bin\Python26\lib\site-packages\django\core\handlers\base.py" in get_response 
    100.      response = callback(request, *callback_args, **callback_kwargs) 
File "C:\wamp\bin\Python26\lib\site-packages\django\contrib\admin\options.py" in wrapper 
    239.     return self.admin_site.admin_view(view)(*args, **kwargs) 
File "C:\wamp\bin\Python26\lib\site-packages\django\utils\decorators.py" in _wrapped_view 
    76.      response = view_func(request, *args, **kwargs) 
File "C:\wamp\bin\Python26\lib\site-packages\django\views\decorators\cache.py" in _wrapped_view_func 
    69.   response = view_func(request, *args, **kwargs) 
File "C:\wamp\bin\Python26\lib\site-packages\django\contrib\admin\sites.py" in inner 
    190.    return view(request, *args, **kwargs) 
File "C:\wamp\bin\Python26\lib\site-packages\django\utils\decorators.py" in _wrapper 
    21.    return decorator(bound_func)(*args, **kwargs) 
File "C:\wamp\bin\Python26\lib\site-packages\django\utils\decorators.py" in _wrapped_view 
    76.      response = view_func(request, *args, **kwargs) 
File "C:\wamp\bin\Python26\lib\site-packages\django\utils\decorators.py" in bound_func 
    17.     return func(self, *args2, **kwargs2) 
File "C:\wamp\bin\Python26\lib\site-packages\django\db\transaction.py" in _commit_on_success 
    299.      res = func(*args, **kw) 
File "C:\wamp\bin\Python26\lib\site-packages\django\contrib\admin\options.py" in add_view 
    777.    if form.is_valid(): 
File "C:\wamp\bin\Python26\lib\site-packages\django\forms\forms.py" in is_valid 
    121.   return self.is_bound and not bool(self.errors) 
File "C:\wamp\bin\Python26\lib\site-packages\django\forms\forms.py" in _get_errors 
    112.    self.full_clean() 
File "C:\wamp\bin\Python26\lib\site-packages\django\forms\forms.py" in full_clean 
    269.   self._post_clean() 
File "C:\wamp\bin\Python26\lib\site-packages\django\forms\models.py" in _post_clean 
    345.    self.validate_unique() 
File "C:\wamp\bin\Python26\lib\site-packages\django\forms\models.py" in validate_unique 
    354.    self.instance.validate_unique(exclude=exclude) 
File "C:\wamp\bin\Python26\lib\site-packages\django\db\models\base.py" in validate_unique 
    695.   date_errors = self._perform_date_checks(date_checks) 
File "C:\wamp\bin\Python26\lib\site-packages\django\db\models\base.py" in _perform_date_checks 
    802.     lookup_kwargs['%s__day' % unique_for] = date.day 

Exception Type: AttributeError at /admin/editoriales/editorial/add/ 
Exception Value: 'NoneType' object has no attribute 'day' 

UPDATE 型號聯繫:

from django.contrib import admin 
from myproject.editoriales.models import Editorial 

    class EditorialAdmin(admin.ModelAdmin): 
     prepopulated_fields = {'slug': ['titulo']} 
     list_display = ('titulo', 'pub_date', 'autor', 'activa') 
     list_per_page = 10 
     date_hierarchy = 'pub_date' 
     search_fields = ['titulo', 'editorial', ] 
     list_filter = ('pub_date', 'activa', 'autor',) 

     class Media: 
      js = ('/public/tiny_mce/tiny_mce.js',    
        '/public/tiny_mce/textareas.js',    
       ) 

    admin.site.register(Editorial, EditorialAdmin) 

謝謝你們

對不起我的英語

+0

你能告訴我們完整的錯誤信息和堆棧跟蹤嗎? – 2010-06-07 04:50:37

+0

當然,我編輯和粘貼完整的錯誤 – Asinox 2010-06-07 05:25:38

+0

您是否使用ModelAdmin類?如果是這樣,你可以發佈它嗎? – 2010-06-07 07:41:20

回答

2

我認爲問題是unique_for_date ='pub_date'與pub_date一起是auto_now_add = True。 只有在之後,auto_now_add才能工作已完成保存。檢查unique_for_date 之前的保存。 檢查完成後,pub_date字段仍然爲無,因此檢查失敗

提出瞭解決方案:從unique_for_date

  1. 變化蛞蝓,以獨特的(但你不能有在不同的日期相同塞2篇)。
  2. 將pub_date從auto_now_add更改爲default = datetime.date.today(但用戶可以手動更改該值)。
  3. 與2相同,但也使用editable = False。
+0

感謝Ofri Raviv,正在工作,並且我學到了新的東西:) – Asinox 2010-06-08 02:10:56

0

試圖取代:

def save(self,force_insert=False, force_update=False): 
    from markdown import markdown 
    if self.editorial: 
     self.editorial_html = markdown(self.editorial) 
    super(Editorial,self).save(force_insert,force_update) 

有:

def save(self, *args, **kwargs): 
    from markdown import markdown 
    if self.editorial: 
     self.editorial_html = markdown(self.editorial) 
    super(Editorial,self).save(*args, **kwargs) 

請注意最後一行中的差異。你用兩個具有False值的位置參數調用save函數(它們可能應該是關鍵字參數,在這種情況下它應該看起來像super(Editorial,self).save(force_insert=force_insert,force_update=force_update)) ,並限制了可以傳遞給Django的save方法的可能選項,重寫。上面的第二個代碼片段解決了這兩個問題。

+0

感謝John Debs的幫助,但我有同樣的錯誤:( – Asinox 2010-06-07 06:19:52

相關問題