0
私はブック持っていると、別のモデルは、どのように私はその後、私のフォームは次のようになり多対多のModelForm
class Category(models.Model):
name = models.CharField(max_length = 255)
class Book(models.Model):
name = models.CharField(max_length = 255)
categories = models.ManyToManyField(Category, through='book.BookCategory')
class BookCategory(models.Model):
business = models.ForeignKey(Business)
category = models.ForeignKey(Category)
user = models.ForeignKey(User)
created = models.DateField()
スルー多対多の関係を持つカテゴリーモデルにウィジェットを変更します:
class BookForm(forms.ModelForm):
name = forms.CharField(max_length = 255)
categories = forms.ModelMultipleChoiceField(
widget=forms.CheckboxSelectMultiple,
queryset = Category.objects.all()
)
class Meta:
model = Business
fields = ('name', 'categories')
Iをフォームのインスタンスを保存しようとする book_form = BookForm(request.POST、instance = business) book_form.save()
このエラーが発生します: 中間モデルを指定するManyToManyFieldに値を設定できません。代わりにbook.BookCategoryのマネージャを使用してください。
どうすればこの問題を解決できますか?
readl BookCategoryには、誰がいつ何をしたのかを追跡するためのユーザーと作成者があります。これを反映する質問を編集しました – Joelbitar