本文介绍了Django Inline Formsets使用自定义窗体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的模型:
class Author(models.Model):
description = models.CharField(max_length = 100)
class Book(models.Model):
author = models.ForeignKey )
details = models.CharField(max_length = 100)
class AuthorForm(ModelForm):
class Meta:
widgets = {
'description' :Textarea(attrs = {'cols':40,'rows':4}),
}
在我的views.py中,我从AuthorForm中创建了表单,所以
form = AuthorForm(request.POST)
但是我也为图书做了一个表单
InlineFormSet = inlineformset_factory(作者,图书)
不能传递一个带有小部件的BooksForm,所以如何添加一个textarea小部件到书的详细信息。
甚至有可能吗?我错过了一些明显的东西吗?
解决方案
尝试:
class BookForm(ModelForm):
class Meta:
model = Book
widgets = {
'details':Textarea(attrs = {'cols' :40,'rows':4}),
}
InlineFormSet = inlineformset_factory(作者,Book,form = BookForm)
Wtower更新
听起来就像现在可以调用
inlineformset_factory(Author,Book,widgets = {'details':Textarea(attrs = {'cols':40}))
I am using inline formsets.
My model:
class Author(models.Model):
description = models.CharField(max_length=100)
class Book(models.Model):
author = models.ForeignKey(Author)
details = models.CharField(max_length=100)
class AuthorForm(ModelForm):
class Meta:
widgets = {
'description': Textarea(attrs={'cols': 40, 'rows': 4}),
}
In my views.py I made the form from the AuthorForm like so
form = AuthorForm(request.POST)
But I also made a formset for the Books
InlineFormSet = inlineformset_factory(Author, Books)
I cannot pass in a BooksForm with widgets, so how do I add a textarea widget to the book details.
Is it even possible? Am I missing something obvious?
解决方案
Try:
class BookForm(ModelForm):
class Meta:
model = Book
widgets = {
'details': Textarea(attrs={'cols': 40, 'rows': 4}),
}
InlineFormSet = inlineformset_factory(Author, Book, form=BookForm)
Update by Wtower
Sounds like you can now call
inlineformset_factory(Author, Book, widgets={'details': Textarea(attrs={'cols': 40}))
这篇关于Django Inline Formsets使用自定义窗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!