gpt4 book ai didi

python - 我如何处理这个错误 : __init__() got an unexpected keyword argument 'book_category'

转载 作者:行者123 更新时间:2023-12-04 08:27:33 26 4
gpt4 key购买 nike

我正在尝试输入一些书籍数据并使用 django 将其保存到我的数据库中(试图制作一个网络图书馆)
但是我遇到了这种错误:
初始化 () 得到了一个意外的关键字参数“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 开发新手,我已经坚持了好几天了。我真的很感激一些帮助:)

最佳答案

当您调用以下内容时,问题出在代码中的多个实例中:

book_category__icontains 
从您的模型中可以看出,您没有 book_category领域,你拥有的是 book_category_no field 。
但在尝试更改此设置之前,您可能需要重命名模型字段。这里有些例子。好的名字会让调试更容易。
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"

关于python - 我如何处理这个错误 : __init__() got an unexpected keyword argument 'book_category' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65191990/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com