0

次のように、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()

2 番目のモデル:

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']

要点は、両方のフォームを 1 つのフォームにまとめ、もちろん 1 つの送信を行いたいということです。したがって、単一の ModelForm 型クラスを使用する必要があると思います。しかし、それは機能しません (専門知識を CvForm にも入れた場合)。私のフォームはModelFormクラスから自動的に作成されるので、単一のクラスを作成したい=>単一のフォームです。

助けてください、どうもありがとう

4

1 に答える 1

1

Expertise から CV への外部キーがあるため、CV ごとに複数の Expertise が存在する可能性があります。そのため、インライン フォームセットを使用して有効にする必要があります。

于 2010-05-20T11:50:01.537 に答える