django使用单个ModelForm从2个表插入数据

发布于 2024-09-02 09:26:35 字数 1843 浏览 1 评论 0原文

我想使用 ModelForm 将两个表中的数据获取到一个表单中,如下所示:

拳头模型:

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 类自动创建的,这就是为什么我想创建一个类 = >单一形式。

请帮忙, 多谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柒夜笙歌凉 2024-09-09 09:26:35

由于从专业知识到简历存在一个外键,因此每个简历可能有多种专业知识。因此,您应该使用 内联表单集 来启用此功能。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文