[英]How to add child field to ModelForm in Django?
我将尽速描述问题。 我有2个型号:
class ParentModel(models.Model):
name = models.CharField(max_length = 30)
surname = models.CharField(max_length = 30)
class ChildModel(models.Model):
pid = models.ForeignKey(ParentModel)
phone = models.CharField(max_length=10, validators=[RegexValidator(r'[0-9]{10}', 'invalid number',)],)
我正在尝试使用ModelForm创建CreateView:
class NewBranchForm(forms.ModelForm):
def __init__(self, request, *args, **kwargs):
self.request = request
super(NewBranchForm, self).__init__(*args, **kwargs)
class Meta:
model = ParentModel
fields = ['name', 'surname']
def save(self):
...
如果我的模板看起来像这样,如何从ChildModel放入电话的模板字段:
<form action="" method="post">
{% csrf_token %}
{{ form.as_p }}<br>
<input type="submit" value="Create new branch" />
感谢和抱歉,我的英语不是我的母语
您应该为电话创建一个ModelFormset 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.