问题描述
我想使用ModelForm从2个表格中获取数据,如下所示:
fist模型:
class Cv(models.Model):
pre>
created_by = models.ForeignKey(User,blank = True)
first_name = models.CharField '),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 a 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)
specialization = 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 '),max_length = 30,blank = True)
references = models.CharField(('references'),max_length = 30,blank = True)
workplace = models.CharField ,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','specialization']
将这两种形式整合成一个单一的形式,当然是单一的。所以,我想我应该使用一个单一的ModelForm类型类。但它不工作(如果我把CvForm的专业知识)。
我的表单是从ModelForm类自动创建的,这就是为什么我要做一个单一的类=>一个单一的表单。
帮助plss,
感谢很多解决方案由于有一个从Expertise到CV的外键,每个cv可能有多个专家。因此,您应该使用 。
I want to get data from 2 tables into a form using ModelForm, like this:
fist model:
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()
second model:
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)
then i have in 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']
The point is that i would want to have the both forms into one single form, and a single submit of course. So, i guess i should use a single ModelForm type class. But it doesn't work (if i put the expertise in the CvForm too ).My form is created automatically from the ModelForm class, that's why i want to make a single class = > a single form.
Help plss,Thanks a lot
解决方案Since there is a foreign key from Expertise to CV, there are potentially multiple expertises for each cv. So you should enable that by using inline formsets.
这篇关于django使用单个ModelForm从2个表中插入数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!