如何处理此错误:__init__() 有一个意外的关键字参数“book_category”
Posted
技术标签:
【中文标题】如何处理此错误:__init__() 有一个意外的关键字参数“book_category”【英文标题】:How do I handle this error : __init__() got an unexpected keyword argument 'book_category' 【发布时间】:2021-03-19 08:45:57 【问题描述】:我正在尝试输入一些书籍数据并使用 django 将其保存到我的数据库中(尝试制作网络图书馆)
但是我遇到这种错误:
init() 得到了一个意外的关键字参数“book_category”
错误似乎在这行代码中:
form = CategoryForm(book_category= book_category)
这是我的views.py的代码
class AddBook(View):
def post(self, request):
form = BooksForm(request.POST, request.FILES)
if form.is_valid()
book_category = request.POST.get('book_category')
firstname = request.POST.get('Firstname')
lastname = request.POST.get('Lastname')
author = Author.objects.filter(Q(firstname__icontains = firstname) & Q(lastname__icontains = lastname))
if author:
print(author)
else:
form = AuthorForm(firstname=firstname, lastname=lastname)
form.save()
category = Category.objects.filter(Q(book_category__icontains = book_category))
if category:
print(category)
else:
form = CategoryForm(book_category= book_category)
form.save()
author = Author.objects.filter(Q(firstname__icontains = firstname) & Q(lastname__icontains = lastname))
for a in author:
print(a.book_author_id)
for c in category:
print(c.book_category_no)
book_title = request.POST.get('book_title')
book_cover = request.FILES.get('book_cover')
book_file = request.FILES.get('book_file')
book_year = request.POST.get('book_year')
book_tags = request.POST.get('book_tags')
book_summary = request.POST.get('book_summary')
form = Books(book_title = book_title, book_author_id = Author.objects.get(book_author_id = a.book_author_id), book_cover = book_cover,
book_file = book_file, book_year = book_year, book_summary = book_summary, book_category_no = Category.objects.get(book_category_no = c.book_category_no),
is_bookmarked = 0, is_downloaded = 0, is_read = 0, is_deleted = 0)
form.save()
return HttpResponse('Book Saved!')
else:
print(form.errors)
return HttpResponse('Not Valid')
这是我的models.py:
class Category(models.Model):
book_category_no = models.AutoField(primary_key=True)
book_category = models.CharField(max_length=100)
class Meta:
db_table = "Category"
class Books(models.Model):
book_id = models.AutoField(primary_key=True)
book_title = models.CharField(max_length = 100)
book_author_id = models.ForeignKey(Author, on_delete=models.CASCADE)
book_cover = models.ImageField(upload_to='media/')
book_file = models.FileField(upload_to='media/')
book_year = models.DateField()
book_tags = models.CharField(max_length = 100)
book_summary = models.CharField(max_length = 100)
book_category_no = models.ForeignKey(Category, on_delete=models.CASCADE)
date_added = models.DateField(default=timezone.now)
# book_info = models.CharField(max_length = 100, default="")
is_bookmarked = models.BooleanField()
is_downloaded = models.BooleanField()
is_read = models.BooleanField()
is_deleted= models.BooleanField(default=False)
class Meta:
db_table = "Books"
我不知道 Web 开发的新手发生了什么,我已经坚持了好几天了。我真的很感激一些帮助:)
【问题讨论】:
【参考方案1】:当您调用以下代码时,问题出在代码中的多个实例:
book_category__icontains
从您的模型中可以看出,您没有book_category
字段,您拥有的是book_category_no
字段。
但在尝试更改此设置之前,您可能需要重命名模型字段。这里有些例子。好的名字会让调试更容易。
class Category(models.Model):
# book_category_no = models.AutoField(primary_key=True) # no need for this.
# book_category = models.CharField(max_length=100)
name = models.CharField(max_length=100
class Meta:
db_table = "Category"
class Books(models.Model):
#book_id = models.AutoField(primary_key=True)# no need for this
title = models.CharField(max_length = 100)
author= models.ForeignKey(Author, on_delete=models.CASCADE)# do not use author_id, use just author
# similarly:
cover = models.ImageField(upload_to='media/')
file = models.FileField(upload_to='media/')
year = models.DateField()
tags = models.CharField(max_length = 100)
summary = models.CharField(max_length = 100)
#when referring foreignkey, no need to add _no or _id after the name.
category = models.ForeignKey(Category, on_delete=models.CASCADE)
date_added = models.DateField(default=timezone.now)
# book_info = models.CharField(max_length = 100, default="")
is_bookmarked = models.BooleanField()
is_downloaded = models.BooleanField()
is_read = models.BooleanField()
is_deleted= models.BooleanField(default=False)
class Meta:
db_table = "Books"
【讨论】:
哇,非常感谢!有效!我会按照你所说的关于名称的提示进行操作。以上是关于如何处理此错误:__init__() 有一个意外的关键字参数“book_category”的主要内容,如果未能解决你的问题,请参考以下文章
错误:__init__() 有一个意外的关键字参数“n_splits”
Django Channels __init__() 有一个意外的关键字参数“范围”
Tensorflow 错误:TypeError:__init__() 得到了一个意外的关键字参数“dct_method”[关闭]
如何使用 keras 加载保存的模型? (错误: : TypeError: __init__() 得到了一个意外的关键字参数“可训练”)