繁体   English   中英

如何为用户可以选择添加更多字段的表单创建 Django model?

[英]How can I create a Django model for a form in which users can choose to add more fields?

这是写出models.pyforms.py中所有字段的正确方法吗? 我确信必须有一种更简洁的方法来做到这一点,而不是单独列出所有字段并在最后用数字区分它们。

我研究了 Django FormSets,但我不确定如何将它与 Django WizardForms 一起使用。

谢谢

模型.py

class Profile(models.Model):
    education_school            = models.CharField(max_length=500, null=True, blank=True)
    education_degree            = models.CharField(max_length=500, null=True, blank=True)
    education_field             = models.CharField(max_length=100, null=True, blank=True)
    education_start             = models.CharField(max_length=100, null=True, blank=True)
    education_end               = models.CharField(max_length=100, null=True, blank=True)
    education_grade             = models.TextField(null=True, blank=True)
    education_description       = models.TextField(null=True, blank=True)

    education_school2           = models.CharField(max_length=500, null=True, blank=True)
    education_degree2           = models.CharField(max_length=500, null=True, blank=True)
    education_field2            = models.CharField(max_length=100, null=True, blank=True)
    education_start2            = models.CharField(max_length=100, null=True, blank=True)
    education_end2              = models.CharField(max_length=100, null=True, blank=True)
    education_grade2            = models.TextField(null=True, blank=True)
    education_description2      = models.TextField(null=True, blank=True)

    education_school3           = models.CharField(max_length=500, null=True, blank=True)
    education_degree3           = models.CharField(max_length=500, null=True, blank=True)
    education_field3            = models.CharField(max_length=100, null=True, blank=True)
    education_start3            = models.CharField(max_length=100, null=True, blank=True)
    education_end3              = models.CharField(max_length=100, null=True, blank=True)
    education_grade3            = models.TextField(null=True, blank=True)
    education_description3      = models.TextField(null=True, blank=True)

    education_school4           = models.CharField(max_length=500, null=True, blank=True)
    education_degree4           = models.CharField(max_length=500, null=True, blank=True)
    education_field4            = models.CharField(max_length=100, null=True, blank=True)
    education_start4            = models.CharField(max_length=100, null=True, blank=True)
    education_end4              = models.CharField(max_length=100, null=True, blank=True)
    education_grade4            = models.TextField(null=True, blank=True)
    education_description4      = models.TextField(null=True, blank=True)

    education_school5           = models.CharField(max_length=500, null=True, blank=True)
    education_degree5           = models.CharField(max_length=500, null=True, blank=True)
    education_field5            = models.CharField(max_length=100, null=True, blank=True)
    education_start5            = models.CharField(max_length=100, null=True, blank=True)
    education_end5              = models.CharField(max_length=100, null=True, blank=True)
    education_grade5            = models.TextField(null=True, blank=True)
    education_description5      = models.TextField(null=True, blank=True)

forms.py

class ProfileFour(forms.Form):
    education_school        = forms.CharField()
    education_degree        = forms.CharField()
    education_field         = forms.CharField()
    education_start         = forms.ChoiceField(choices=years)
    education_end           = forms.ChoiceField(choices=years)    
    education_grade         = forms.CharField(widget=forms.Textarea)
    education_description   = forms.CharField(widget=forms.Textarea)

    education_school2       = forms.CharField()
    education_degree2       = forms.CharField()
    education_field2        = forms.CharField()
    education_start2        = forms.ChoiceField(choices=years)
    education_end2          = forms.ChoiceField(choices=years)    
    education_grade2        = forms.CharField(widget=forms.Textarea)
    education_description2  = forms.CharField(widget=forms.Textarea)

    education_school3       = forms.CharField()
    education_degree3       = forms.CharField()
    education_field3        = forms.CharField()
    education_start3        = forms.ChoiceField(choices=years)
    education_end3          = forms.ChoiceField(choices=years)    
    education_grade3        = forms.CharField(widget=forms.Textarea)
    education_description3  = forms.CharField(widget=forms.Textarea)

    education_school4       = forms.CharField()
    education_degree4       = forms.CharField()
    education_field4        = forms.CharField()
    education_start4        = forms.ChoiceField(choices=years)
    education_end4          = forms.ChoiceField(choices=years)    
    education_grade4        = forms.CharField(widget=forms.Textarea)
    education_description4  = forms.CharField(widget=forms.Textarea)

    education_school5       = forms.CharField()
    education_degree5       = forms.CharField()
    education_field5        = forms.CharField()
    education_start5        = forms.ChoiceField(choices=years)
    education_end5          = forms.ChoiceField(choices=years)    
    education_grade5        = forms.CharField(widget=forms.Textarea)
    education_description5  = forms.CharField(widget=forms.Textarea)

您在 django 中有 ModelForm

class ProfileFour(form.ModelForm)
    class Meta:
       model = Profile
       fields = '__all__'  # you wand all field
       # or 
       fields = ['education_school', 'education_degree' ..] # name the field you want

doc django 真的很好,请阅读。

https://docs.djangoproject.com/fr/3.0/topics/forms/modelforms/

编辑:完成@Countour-Integral 的答案

class Profil(models.Model)
   username = models.charField()
   ...

class Education(models.Model)
    profil = models.ForeignKey(Profil)
    school = models.CharField()
    degree = models.IntegerField()
    field = models.CharField()
    start = models.DateField()
    end = models.DateField()
    grade = models.CharField()
    description = models.textField()

你以错误的方式看待这个问题。 首先,您的 model 应该更新以反映您的用例。 如果您不提前知道字段,可以使用JSONField中包含的ArrayField ,例如

education = ArrayField(models.JSONField(default=dict), size=10, default=list)

现在我们已经解决了这个问题,做你想做的更好的方法是将Education设置为 model,然后Profile model 将具有Education的外键。 IE

class Education(models.Model):
  school = models.CharField(max_length=50)
  degree = models.CharField(max_length=50)
  ...rest of fields

那么您的Profile model 将具有: education = models.ForeignKey(Education, related_name="profile")

要获得不同数量的教育 forms,您应该利用modelformset EducationForm创建为普通的 modelForm 实例,然后:

EducationFormSet = modelformset_factory(Education, form=EducationForm, max_num=10, extra=1)

您可以只使用 ManyToMany 字段,而不是编写相同的代码 5 次。

class Profile(models.Model):
    education_school            = models.ManyToManyField( blank=True)
    education_degree            = models.ManyToManyField( blank=True)
    education_field             = models.ManyToManyField( blank=True)
    education_start             = models.ManyToManyField( blank=True)
    education_end               = models.ManyToManyField( blank=True)
    education_grade             = models.ManyToManyField( blank=True)
    education_description       = models.ManyToManyField( blank=True)

  • 然后你可以分别定义每个字段
class education_school(models.Model):
    education_school = models.CharField(max_length=500, null=True, blank=True)

等等与其他领域

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM