我不知道我的错误在哪里,但是Django 1.2.1给出了这个错误:当我尝试从管理员区域保存表单时,'NoneType'对象没有属性'day'
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)
我不知道为什么会出现这个错误,
已完成错误:
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 :(得分:2)
我认为问题是将unique_for_date ='pub_date'与pub_date一起使用,即auto_now_add = True。 auto_now_add仅在 保存完成后才有效。 unique_for_date在保存之前检查。 检查完成后,pub_date字段仍然为None,因此检查失败。
建议的解决方案:答案 1 :(得分: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的保存方法的可能选项,你要覆盖它。上面的第二个代码片段解决了这两个问题。