我写的代码(只贴主要部分):
admin.py
from django.contrib import admin
from .models import Tag, Category, Article
class ArticleAdmin(admin.ModelAdmin):
def save_model(self, request, obj, form, change):
obj.author = request.user
obj.save()
admin.site.register(Tag)
admin.site.register(Category)
admin.site.register(Article)
models.py
from django.db import models
from django.contrib.auth.models import User
class Tag(models.Model):
class Meta:
verbose_name = '标签'
verbose_name_plural = verbose_name
tag_name = models.CharField('标签名', max_length=20)
create_time = models.DateTimeField('创建时间', auto_now_add=True)
def __str__(self):
return self.tag_name
class Category(models.Model):
class Meta:
verbose_name = '分类'
verbose_name_plural = verbose_name
name = models.CharField('分类名', max_length=20)
def __str__(self):
return self.name
class Article(models.Model):
class Meta:
verbose_name = '文章'
verbose_name_plural = verbose_name
ordering = ['-updated_time', '-created_time']
title = models.CharField('标题', max_length=60)
content = models.TextField('正文')
category = models.ManyToManyField(Category, verbose_name='分类', blank=True)
author = models.ForeignKey(User, verbose_name='作者', editable=False)
tags = models.ForeignKey(Tag, verbose_name="标签", related_name='tags_article', blank=True)
view_count = models.IntegerField('浏览次数', editable=False,default=0)
created_time = models.DateTimeField('添加时间', auto_now_add=True)
updated_time = models.DateTimeField('修改时间', auto_now=True)
def __str__(self):
return self.title
在后台增加文章,抛出错误:
Internal Server Error: /admin/blog/article/add/
Traceback (most recent call last):
File "C:\Users\cxgre\fancyblog\lib\site-packages\django\db\backends\utils.py", line 65, in execute
return self.cursor.execute(sql, params)
File "C:\Users\cxgre\fancyblog\lib\site-packages\django\db\backends\sqlite3\base.py", line 328, in execute
return Database.Cursor.execute(self, query, params)
sqlite3.IntegrityError: NOT NULL constraint failed: blog_article.author_id
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\cxgre\fancyblog\lib\site-packages\django\core\handlers\exception.py", line 41, in inner
response = get_response(request)
File "C:\Users\cxgre\fancyblog\lib\site-packages\django\core\handlers\base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\cxgre\fancyblog\lib\site-packages\django\core\handlers\base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\cxgre\fancyblog\lib\site-packages\django\contrib\admin\options.py", line 551, in wrapper
return self.admin_site.admin_view(view)(*args, **kwargs)
File "C:\Users\cxgre\fancyblog\lib\site-packages\django\utils\decorators.py", line 149, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "C:\Users\cxgre\fancyblog\lib\site-packages\django\views\decorators\cache.py", line 57, in _wrapped_view_func
response = view_func(request, *args, **kwargs)
File "C:\Users\cxgre\fancyblog\lib\site-packages\django\contrib\admin\sites.py", line 224, in inner
return view(request, *args, **kwargs)
File "C:\Users\cxgre\fancyblog\lib\site-packages\django\contrib\admin\options.py", line 1508, in add_view
return self.changeform_view(request, None, form_url, extra_context)
File "C:\Users\cxgre\fancyblog\lib\site-packages\django\utils\decorators.py", line 67, in _wrapper
return bound_func(*args, **kwargs)
File "C:\Users\cxgre\fancyblog\lib\site-packages\django\utils\decorators.py", line 149, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "C:\Users\cxgre\fancyblog\lib\site-packages\django\utils\decorators.py", line 63, in bound_func
return func.__get__(self, type(self))(*args2, **kwargs2)
File "C:\Users\cxgre\fancyblog\lib\site-packages\django\contrib\admin\options.py", line 1408, in changeform_view
return self._changeform_view(request, object_id, form_url, extra_context)
File "C:\Users\cxgre\fancyblog\lib\site-packages\django\contrib\admin\options.py", line 1448, in _changeform_view
self.save_model(request, new_object, form, not add)
File "C:\Users\cxgre\fancyblog\lib\site-packages\django\contrib\admin\options.py", line 979, in save_model
obj.save()
File "C:\Users\cxgre\fancyblog\lib\site-packages\django\db\models\base.py", line 806, in save
force_update=force_update, update_fields=update_fields)
File "C:\Users\cxgre\fancyblog\lib\site-packages\django\db\models\base.py", line 836, in save_base
updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "C:\Users\cxgre\fancyblog\lib\site-packages\django\db\models\base.py", line 922, in _save_table
result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "C:\Users\cxgre\fancyblog\lib\site-packages\django\db\models\base.py", line 961, in _do_insert
using=using, raw=raw)
File "C:\Users\cxgre\fancyblog\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\cxgre\fancyblog\lib\site-packages\django\db\models\query.py", line 1063, in _insert
return query.get_compiler(using=using).execute_sql(return_id)
File "C:\Users\cxgre\fancyblog\lib\site-packages\django\db\models\sql\compiler.py", line 1099, in execute_sql
cursor.execute(sql, params)
File "C:\Users\cxgre\fancyblog\lib\site-packages\django\db\backends\utils.py", line 80, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "C:\Users\cxgre\fancyblog\lib\site-packages\django\db\backends\utils.py", line 65, in execute
return self.cursor.execute(sql, params)
File "C:\Users\cxgre\fancyblog\lib\site-packages\django\db\utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "C:\Users\cxgre\fancyblog\lib\site-packages\django\utils\six.py", line 685, in reraise
raise value.with_traceback(tb)
File "C:\Users\cxgre\fancyblog\lib\site-packages\django\db\backends\utils.py", line 65, in execute
return self.cursor.execute(sql, params)
File "C:\Users\cxgre\fancyblog\lib\site-packages\django\db\backends\sqlite3\base.py", line 328, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.IntegrityError: NOT NULL constraint failed: blog_article.author_id
我该怎么做呢?
admin.py中定义了
ArticleAdmin
但并没有绑定注册。 所以ArticleAdmin
没有生效。 需要改成如下