如何在django多对多关系中将从前端获取的多个值保存到数据库中?

f8rj6qna  于 2023-05-30  发布在  Go
关注(0)|答案(1)|浏览(117)

在添加一本书时,我必须从前端获取数据。我创建了一个AddBookForm表单。BookAuthor具有多对多关系。从前端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吗?请帮我解决这个问题。谢谢你

cbjzeqam

cbjzeqam1#

你好,我的朋友,首先你只需要覆盖保存在您的形式:
所以会是这样

def save(self, commit=True):
    book = super().save(commit=False) # get the instance
    if commit:
        book.save()# normally save the book
    authors = self.cleaned_data.get('authors') # get the authors (ids also valid)
    if authors:
        book.authors_set.set(authors) # set the authors to the book
    return book

相关问题