gpt4 book ai didi

具有多个外键的 Django 内联表单

转载 作者:行者123 更新时间:2023-12-02 01:47:52 29 4
gpt4 key购买 nike

我正在尝试在我的模板中使用 Django 的 ModelForm 和内联表单。但是,我找不到任何文档可以巧妙地映射到具有多个外键的数据库模型返回到同一个表。这些是我的模型:

# models.py

class Universities(models.Model):
name = models.CharField(max_length=100)

class Majors(models.Model):
name = models.CharField(max_length=80)

class Resumes(models.Model):
name = models.CharField(max_length=70)
undergrad = models.ForeignKey(Universities, related_name='undergrad_university')
undergrad_major = models.ForeignKey(Majors, related_name='undergrad_major')
grad = models.ForeignKey(Universities, related_name='grad_university')
grad_major = models.ForeignKey(Majors, related_name='grad_major')

我如何让 Django 生成一个表单来提交简历,用户可以在其中输入他们的大学名称和专业?在保存新简历之前,所有四个都将用于在各自的数据库中创建新条目(2 个在大学,2 个在专业),类似于 inline formset example适用于单个外键。

最佳答案

EDIT2:用于制作表格。我想我已经完成了一个覆盖 save() 方法的个性化表单,就像这样(forms.py):

class YourForm(forms.Form):
fname = forms.CharField(label="name",max_length=70,validators=[#Choose your validators here])
fundergrad = forms.CharField(label="fundergrad",max_length=100,validators=[#Choose your validators here])
fundergrad_major = forms.CharField(label="fundergrad_major",max_length=80,validators=[#Choose your validators here])
fgrad = forms.CharField(label="fgrad",max_length=100,validators=[#Choose your validators here])
fgrad_major = forms.CharField(label="fgrad_major",max_length=80,validators=[#Choose your validators here])

def save(self, datas):
res = Resumes()
res.name = datas['fname']

undergrad = Universities()
undergrad_major = Majors()
grad = Universities()
grad_major = Majors()

undergrad.name = datas['fundergrad']
undegrad_major.name = datas['fundergrad_major']
grad.name = datas['fgrad']
grad_major.name = datas['fgrad_major']

undergrad.save()
undergrad_major.save()
grad.save()
grad_major.save()

res.undergrad = undergrad
res.undergrad_major = undergrad_major
res.grad = grad
res.grad_major = grad_major

res.save()
return res
在 views.py 中:
def formView(request) :
if request.method == 'POST':
form = YourForm(request.POST)
if form.is_valid():
datas={}
datas['fundergrad']=form.cleaned_data['fundergrad']
datas['fundergrad_major']=form.cleaned_data['fundergrad_major']
datas['fgrad']=form.cleaned_data['fgrad']
datas['fgrad_major']=form.cleaned_data['fgrad_major']

form.save(datas)

#Then do what you have to do in your view

EDIT1 :(不回答问题,但它可能对某人有帮助,所以我放在这里)
我会在 admin.py 中尝试这样的事情:
class UniversitiesInline1(admin.StackedInline):
model = Universities
fk_name = "undergrad"

class UniversitiesInline2(admin.StackedInline):
model = Universities
fk_name = "grad"

class MajorsInline1(admin.StackedInline):
model = Majors
fk_name = "undergrad_major"

class MajorsInline2(admin.StackedInline):
model = Majors
fk_name = "grad_major"

class ResumesAdmin(admin.ModelAdmin)
inlines = [
UniversitiesInline1,
UniversitiesInline2,
MajorsInline1,
MajorsInline2,
]

admin.site.register(Resumes, ResumesAdmin)
说明: https://docs.djangoproject.com/en/dev/ref/contrib/admin/#working-with-a-model-with-two-or-more-foreign-keys-to-the-same-parent-model

关于具有多个外键的 Django 内联表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24493145/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com