Django:处理模板中的外键值



我最近开始使用Django,我很难在一个表单中使用两个模型。我在stackoverflow中查找了很多问题,但找不到任何答案。

我有两个模型-学生&地址

这是我的型号.py

class Students (models.Model):
Student_Name = models.CharField(max_length=50, db_column='Student_Name')
College_Number = models.CharField(
max_length=10, db_column='College_Number')
Gender = models.CharField(max_length=50, db_column='Gender')
Blood_Group = models.CharField(max_length=50, db_column='Blood_Group')
Set = models.CharField(max_length=50, db_column='Set')
Standard = models.CharField(max_length=50, db_column='Standard')

class Addresses (models.Model):
Student_ID = models.ForeignKey(
Students, on_delete=models.CASCADE, db_column='Student_ID')
City = models.CharField(max_length=50, db_column='City')
State = models.CharField(max_length=50, db_column='State')
Pincode = models.CharField(max_length=50, db_column='Pincode')
AddressLine1 = models.CharField(max_length=1024, db_column='AddressLine1')
AddressLine2 = models.CharField(max_length=1024, db_column='AddressLine2')

这是我的观点.py

def student_create_view(request):
student_form = StudentFrom(request.POST or None)
address_form = AddressForm(request.POST or None)
if (request.POST.get('Student_Name')
and request.POST.get('College_Number')
and request.POST.get('Gender')
and request.POST.get('Blood_Group')
and request.POST.get('Set')
and request.POST.get('Standard')
and request.POST.get('AddressLine1')
and request.POST.get('AddressLine2')
and request.POST.get('City')
and request.POST.get('State')
and request.POST.get('Pincode')
and student_form.is_valid()
and address_form.is_valid()):
# Fetch values from the Student Form
student_form.name = request.POST.get('Student_Name')
student_form.collegenumber = request.POST.get('College_Number')
student_form.Gender = request.POST.get('Gender')
student_form.Blood_Group = request.POST.get('Blood_Group')
student_form.Set = request.POST.get('Set')
student_form.Standard = request.POST.get('Standard')
# Fetch values from the Address Form
address_form.AddressLine1 = request.POST.get('AddressLine1')
address_form.AddressLine2 = request.POST.get('AddressLine2')
address_form.City = request.POST.get('City')
address_form.State = request.POST.get('State')
address_form.Pincode = request.POST.get('Pincode')
student_form.save()
address_form.save()
return redirect('/create')
else:
print(student_form.errors, address_form.errors)
messages.error(request, student_form.errors)
context = {
'student_form': student_form, 'address_form': address_form
}
return render(request, "students/student_create.html", context)

这是我的表格.py

from django import forms
from .models import Students, Addresses
SET_CHOICES = (
('Bikrams', 'Bikrams'),
('Ranas', 'Ranas'),
('Aryans', 'Aryans'),
('Rajputs', 'Rajputs')
)
GENDER_CHOICES = (
('Male', 'Male'),
('Female', 'Female')
)
BLOODGROUP_CHOICES = (
('A-Positive', 'A-Positive'),
('AB-Positive', 'AB-Positive'),
('B-Positive', 'B-Positive'),
('O-Positive', 'O-Positive'),
('A-Negative', 'A-Negative'),
('AB-Negative', 'AB-Negative'),
('B-Negative', 'B-Negative'),
('O-Negative', 'O-Negative')
)
STANDARD_CHOICES = (
('Nursery', 'Nursery'),
('KG1', 'KG1'),
('KG2', 'KG2'),
('Class1', 'Class1'),
('Class2', 'Class2'),
('Class3', 'Class3'),
('Class4', 'Class4'),
('Class5', 'Class5'),
('Class6', 'Class6'),
('Class7', 'Class7'),
('Class8', 'Class8'),
('Class9', 'Class9'),
('Class10', 'Class10'),
('Class11', 'Class11'),
('Class12', 'Class12')
)

class StudentFrom(forms.ModelForm):
Set = forms.ChoiceField(choices=SET_CHOICES,
widget=forms.RadioSelect(attrs={'class': 'ml-2'}))
Gender = forms.ChoiceField(
choices=GENDER_CHOICES, widget=forms.RadioSelect(attrs={'class': 'ml-2'}))
Blood_Group = forms.ChoiceField(choices=BLOODGROUP_CHOICES)
Standard = forms.ChoiceField(choices=STANDARD_CHOICES)
class Meta:
model = Students
fields = ['Student_Name', 'College_Number',
'Gender', 'Blood_Group', 'Set', 'Standard']

class AddressForm(forms.ModelForm):
AddressLine1 = forms.CharField(label='Address Line 1', widget=forms.Textarea(
attrs={'rows': '1', 'cols': '35'}))
AddressLine2 = forms.CharField(label='Address Line 2', widget=forms.Textarea(
attrs={'rows': '1', 'cols': '35'}))
class Meta:
model = Addresses
fields = ['AddressLine1',
'AddressLine2', 'City', 'State', 'Pincode']

我能够成功地将其迁移到mysqldb,并且我可以在Django管理中看到外键关系(1学生可以有许多地址(。但是,我正在尝试使用一个表单填充这两个模型,但我无法做到这一点,因为我不知道如何在Addresses中设置外键值(Student_ID(。

寻找一个解决方案,我可以用Student(Django创建的默认主键(模型中的主键值填充*Addresses中的Student_ID外键字段。

有没有一种方法可以自动做到这一点,这样我就不必在模板中处理这个问题了?

您可以获得student_form.save()的结果,并将其分配给包裹在address_form:中的.instanceStudent_ID

def student_create_view(request):
if request.method == 'POST':
student_form = StudentFrom(request.POST)
address_form = AddressForm(request.POST)
if student_form.is_valid() and address_form.is_valid():
# Fetch values from the Student Form
student =student_form.save()
address_form.instance.Student_ID = student
address_form.save()
return redirect('/create')
else:
print(student_form.errors, address_form.errors)
messages.error(request, student_form.errors)
else:
student_form = StudentFrom()
address_form = AddressForm()
context = {
'student_form': student_form, 'address_form': address_form
}
return render(request, "students/student_create.html", context)

通常情况下,ForeignKey不会以_ID结束,因此您可能需要将字段重命名为student(字段名称是用snake_case而不是用PascalCase写的(,并将其写为:

address_form.instance.student= student

相关内容

  • 没有找到相关文章

最新更新