我想使用ModelForm将2个表中的数据转换为表单,如下所示:
拳头模型:class Cv(models.Model):
created_by = models.ForeignKey(User, blank=True)
first_name = models.CharField(('first name'), max_length=30, blank=True)
last_name = models.CharField(('last name'), max_length=30, blank=True)
url = models.URLField(verify_exists=True)
picture = models.ImageField(help_text=('Upload an image (max %s kilobytes)' %settings.MAX_PHOTO_UPLOAD_SIZE),upload_to='avatar')
bio = models.CharField(('bio'), max_length=180, blank=True)
expertise= models.ForeignKey(Expertise, blank=True)
date_birth = models.DateField()
第二种模式:
class Expertise(models.Model):
user = models.ForeignKey(User, blank=True)
domain = models.CharField(('domain'), max_length=30, blank=True)
specialisation = models.CharField(('specialization'), max_length=30, blank=True)
degree = models.CharField(('degree'), max_length=30, blank=True)
year_last_degree = models.CharField(('year_last_degree'), max_length=30, blank=True)
lyceum = models.CharField(('lyceum'), max_length=30, blank=True)
faculty = models.CharField(('faculty'), max_length=30, blank=True)
references = models.CharField(('references'), max_length=30, blank=True)
workplace = models.CharField(('workplace'), max_length=30, blank=True)
然后我在forms.py
class CvForm(ModelForm):
class Meta:
model = Cv
fields = ['first_name','last_name','url','picture','bio','date_birth']
class ExpertiseForm(ModelForm):
class Meta:
model = Expertise
fields = ['domain','specialisation']
关键是我希望将两种形式合并为一种形式,然后单一提交。所以,我想我应该使用一个ModelForm类型类。但它不起作用(如果我把专业知识也放在CvForm中)。 我的表单是从ModelForm类自动创建的,这就是为什么我要创建一个单独的类=>单一表格。
帮助plss, 非常感谢