在添加一本书时,我必须从前端获取数据。我创建了一个AddBookForm
表单。Book
和Author
具有多对多关系。从前端Form
中,我获得了相应书籍的作者姓名列表(来自动态输入字段)。现在数据已保存,但存储不正确。我知道这不是保存多对多关系数据的最佳方式。请帮助我以最优化的方式来做这件事。models.py:
class Book(models.Model):
name = models.CharField(max_length=200, null=False, blank=False)
description = models.TextField(null=True, blank=True)
no_of_copies = models.IntegerField(default=1, null=True, blank=True)
status = models.CharField(max_length=15, choices=[('Out of Stock', 'Out of Stock'), ('Available', 'Available')], default='Available')
updated_at = models.DateTimeField(auto_now=True)
created_at = models.DateTimeField(auto_now=True)
def __str__(self):
return self.name
class Author(models.Model):
name = models.CharField(max_length=100, null=True, blank=True)
books = models.ManyToManyField(Book)
def __str__(self):
return self.name
class Genre(models.Model):
name = models.CharField(max_length=50, null=True, blank=True)
books = models.ManyToManyField(Book)
def __str__(self):
return self.name
forms.py
class AddBookForm(forms.ModelForm):
authors = forms.CharField(max_length=255, required=False)
genres = forms.CharField(max_length=255, required=False)
class Meta:
model = Book
fields = '__all__'
views.py
def add_book(request):
if request.method == 'POST':
form = AddBookForm(request.POST)
if form.is_valid():
book = form.save(commit=False)
authors = request.POST.getlist('authors')
book.save()
for name in authors:
author, created = Author.objects.get_or_create(name=name)
author.books.add(book)
return redirect("/")
else:
form = AddBookForm()
context = {'form': form}
return render(request, 'book/add_book.html', context)
我想把这本书的作者添加到数据库中。我发现可以使用.set()
,但我不确定如何在此上下文中使用它。你能告诉我是否可以用.set()
来做这件事吗?我应该在Book
模型中创建另一个ManyToManyField
吗?请帮我解决这个问题。谢谢你
1条答案
按热度按时间cbjzeqam1#
你好,我的朋友,首先你只需要覆盖保存在您的形式:
所以会是这样