类型错误:Post() 在处理查询集时在命令行中'body'意外的关键字参数



我正在Django中创建一个博客应用程序,当我在cmd中创建查询集时,我得到了以下错误,

CMD:中的代码

from django.contrib.auth.models import User
>>> from blog.models import Post
>>> user = User.objects.get(username='mratyunjay')
>>> post = Post(title='Another post',
... slug='another-post',
... body='Post body.',
... author=user)

错误:

Traceback (most recent call last):
File "<console>", line 4, in <module>
File "C:UsersComputerDesktopprojectmy_envlibsite-packagesdjangodbmodelsbase.py", line 501, in __init__
raise TypeError("%s() got an unexpected keyword argument '%s'" % (cls.__name__, kwarg))
TypeError: Post() got an unexpected keyword argument 'body'

admin.py:-

from django.contrib import admin
# Register your models here.
from .models import Post
@admin.register(Post)
class PostAdmin(admin.ModelAdmin):
list_display = ( 'title', 'slug', 'author', 'publish', 'status')
list_filter = ( 'status', 'created', 'publish', 'author')
search_fields = ( 'title', 'body' )
prepopulated_fields = {'slug': ('title',)}
raw_id_fields = ('author',)
date_hierarchy = 'publish'
ordering = ( 'status', 'publish' )

型号.py:-

from django.db import models
# Create your models here.
from django.utils import timezone
from django.contrib.auth.models import User
class Post(models.Model):
STATUS_CHOICES = (
('draft', 'Draft'),
('published', 'Published'),
)
title = models.CharField(max_length=250)
slug = models.SlugField(max_length=250, unique_for_date='publish')
author = models.ForeignKey(User, on_delete=models.CASCADE, related_name='blog_posts')
body = models.TextField
publish = models.DateTimeField(default=timezone.now)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
status = models.CharField(max_length=10, choices=STATUS_CHOICES, default='draft')
class Meta:
ordering = ( '-publish', )

def __str__(self):
return self.title

型号错误.py:-

User model imported from django.contrib.auth.models pylint(imported-auth-user) [5,1] 

我是Django的新手,不知道如何解析用户模型,在创建查询集时出现了另一个错误。我该如何解决此问题?

您的models.py文件中缺少body字段((。它应该是这样的。

body = models.TextField()

之后,运行以下命令,

python manage.py makemigrations
python manage.py migrate

然后尝试在命令行中编写代码

最新更新