如何为外键django创建表单
How to create forms for foreign key django
模型,
class Publication(models.Model):
name=models.CharField(max_length=128)
address=models.CharField(max_length=500)
website=models.URLField()
def __unicode__(self):
return self.name
class Book(models.Model):
name=models.CharField(max_length=128)
publication=models.ForeignKey(Publication)
author=models.CharField(max_length=128)
slug=models.SlugField(unique=True)
def __unicode__(self):
return self.name
def save(self,*args,**kwagrs):
self.slug=slugify(self.slug)
super(Book,self).save(*args,**kwagrs)
我尝试为发布对象制作表格。哪个工作正常。但是我无法为 Book 对象制作表格,因为它已作为外键发布。
forms.py,
class PublicationForm(forms.ModelForm):
name = forms.CharField(max_length=128, help_text="Please enter the publication name.")
address = forms.CharField(max_length=128, help_text="Please enter the address for publication.")
website=forms.URLField(max_length=200, help_text="Please enter the URL of publication.")
class Meta:
model = Publication
如何为将发布作为外键的图书对象创建表单。
更新
我已经尝试将书对象的形式设置为,
class BookForm(forms.ModelForm):
name = forms.CharField(max_length=128, help_text="Please enter the name.")
author = forms.CharField(max_length=128, help_text="Please enter the name of the autthor.")
slug = forms.SlugField(help_text="Please enter the slug")
publication = forms.ModelMultipleChoiceField(
queryset=Publication.objects.all()
)
class Meta:
model = Book
fields = ('name', 'author','slug','publication')
但是当我提交表单时它抛出了下面的错误,
Cannot assign "[<Publication: C# in Depth>]": "Book.publication" must be a "Publication" instance.
看看这个ModelChoiceField
publication = forms.ModelChoiceField(queryset=Book.objects.all())
尝试link他们在视图中。使用 save(commit=False) 您可以创建一个等待完成其数据的 Book 对象。当你完成 book 对象时,你可以用它的所有外键保存它
if request.method == 'POST':
bf = BookForm(request.POST)
publication_id = request.POST.get('publication_id',0)
if bf.is_valid():
if publication_id:
book = bf.save(commit=False)
book.publication_id = publication_id
book.save()
else:
# functional error.
else:
# functional error.
当您使用 ModelMultipleChoiceField
时,它以形式给出 Publication
的查询集,并且您在 Book
和 Publication
模型之间使用外键关系,所以模型无法保存 publication
因为它没有得到 publication
对象。所以你可以这样做来解决问题:
class BookForm(forms.ModelForm):
name = forms.CharField(max_length=128, help_text="Please enter the name.")
author = forms.CharField(max_length=128, help_text="Please enter the name of the autthor.")
slug = forms.SlugField(help_text="Please enter the slug")
publication = forms.ChoiceField(
choices=[(x.id,x.name) for x in Publication.objects.all()]
)
def save(self, commit=True):
instance = super().save(commit=False)
pub = self.cleaned_data['publication']
instance.publication = Publication.objects.get(pk=pub)
instance.save(commit)
return instance
class Meta:
model = Book
fields = ('name', 'author','slug')
或者您可以像这样使用 ModelMultipleChoiceField:
class BookForm(forms.ModelForm):
name = forms.CharField(max_length=128, help_text="Please enter the name.")
author = forms.CharField(max_length=128, help_text="Please enter the name of the autthor.")
slug = forms.SlugField(help_text="Please enter the slug")
publication = forms.ModelMultipleChoiceField(
queryset=Publication.objects.all()
)
def save(self, commit=True):
instance = super().save(commit=False)
pub = self.cleaned_data['publication']
instance.publication = pub[0]
instance.save(commit)
return instance
class Meta:
model = Book
fields = ('name', 'author','slug')
模型,
class Publication(models.Model):
name=models.CharField(max_length=128)
address=models.CharField(max_length=500)
website=models.URLField()
def __unicode__(self):
return self.name
class Book(models.Model):
name=models.CharField(max_length=128)
publication=models.ForeignKey(Publication)
author=models.CharField(max_length=128)
slug=models.SlugField(unique=True)
def __unicode__(self):
return self.name
def save(self,*args,**kwagrs):
self.slug=slugify(self.slug)
super(Book,self).save(*args,**kwagrs)
我尝试为发布对象制作表格。哪个工作正常。但是我无法为 Book 对象制作表格,因为它已作为外键发布。
forms.py,
class PublicationForm(forms.ModelForm):
name = forms.CharField(max_length=128, help_text="Please enter the publication name.")
address = forms.CharField(max_length=128, help_text="Please enter the address for publication.")
website=forms.URLField(max_length=200, help_text="Please enter the URL of publication.")
class Meta:
model = Publication
如何为将发布作为外键的图书对象创建表单。
更新
我已经尝试将书对象的形式设置为,
class BookForm(forms.ModelForm):
name = forms.CharField(max_length=128, help_text="Please enter the name.")
author = forms.CharField(max_length=128, help_text="Please enter the name of the autthor.")
slug = forms.SlugField(help_text="Please enter the slug")
publication = forms.ModelMultipleChoiceField(
queryset=Publication.objects.all()
)
class Meta:
model = Book
fields = ('name', 'author','slug','publication')
但是当我提交表单时它抛出了下面的错误,
Cannot assign "[<Publication: C# in Depth>]": "Book.publication" must be a "Publication" instance.
看看这个ModelChoiceField
publication = forms.ModelChoiceField(queryset=Book.objects.all())
尝试link他们在视图中。使用 save(commit=False) 您可以创建一个等待完成其数据的 Book 对象。当你完成 book 对象时,你可以用它的所有外键保存它
if request.method == 'POST':
bf = BookForm(request.POST)
publication_id = request.POST.get('publication_id',0)
if bf.is_valid():
if publication_id:
book = bf.save(commit=False)
book.publication_id = publication_id
book.save()
else:
# functional error.
else:
# functional error.
当您使用 ModelMultipleChoiceField
时,它以形式给出 Publication
的查询集,并且您在 Book
和 Publication
模型之间使用外键关系,所以模型无法保存 publication
因为它没有得到 publication
对象。所以你可以这样做来解决问题:
class BookForm(forms.ModelForm):
name = forms.CharField(max_length=128, help_text="Please enter the name.")
author = forms.CharField(max_length=128, help_text="Please enter the name of the autthor.")
slug = forms.SlugField(help_text="Please enter the slug")
publication = forms.ChoiceField(
choices=[(x.id,x.name) for x in Publication.objects.all()]
)
def save(self, commit=True):
instance = super().save(commit=False)
pub = self.cleaned_data['publication']
instance.publication = Publication.objects.get(pk=pub)
instance.save(commit)
return instance
class Meta:
model = Book
fields = ('name', 'author','slug')
或者您可以像这样使用 ModelMultipleChoiceField:
class BookForm(forms.ModelForm):
name = forms.CharField(max_length=128, help_text="Please enter the name.")
author = forms.CharField(max_length=128, help_text="Please enter the name of the autthor.")
slug = forms.SlugField(help_text="Please enter the slug")
publication = forms.ModelMultipleChoiceField(
queryset=Publication.objects.all()
)
def save(self, commit=True):
instance = super().save(commit=False)
pub = self.cleaned_data['publication']
instance.publication = pub[0]
instance.save(commit)
return instance
class Meta:
model = Book
fields = ('name', 'author','slug')