[英]Django saving data in model through model form and foreign key
I have a model named Doctor and a model named Appoint which have doctor as a foreign key .我有一个名为Doctor的模型和一个名为Appoint的模型,其中有医生作为外键。 and I have a model form to take input for the Appoint model but not for the foreign key in it.
并且我有一个模型表单来为 Appoint 模型输入输入,但不能输入其中的外键。 I am able to link the Doctor model through url but I am not able to save doctor(foreign key) in Appoint model.
我可以通过 url 链接 Doctor 模型,但我无法在 Appoint 模型中保存医生(外键)。
here are the 2 models:-这是 2 个模型:-
``` class Doctor(models.Model):
docid = models.IntegerField(null=True, default=1001)
name = models.CharField(max_length=40)
phone = models.IntegerField()
add = models.TextField()
email = models.EmailField()
category = models.CharField(choices=doc_cat,max_length=20)
price = models.IntegerField()
def __str__(self):
return self.name
class Appoint(models.Model):
f_name = models.CharField(max_length=12)
l_name = models.CharField(max_length=12)
phone1 = models.IntegerField()
phone2 = models.IntegerField()
add = models.CharField(max_length=100)
city = models.CharField(max_length=20)
state = models.CharField(max_length=30)
pincode = models.IntegerField()
doctor = models.ForeignKey(Doctor,null=True, on_delete=models.CASCADE)
day = models.CharField(max_length=30)
timeslot = models.CharField(max_length=30)
symptom = models.CharField(max_length=200)
email = models.EmailField()
date = models.DateField(auto_now=True)
def __str__(self):
return self.f_name + self.l_name```
here is the view method:-这是查看方法:-
``` def takeappointment(request, docid):
doctor = Doctor.objects.get(docid = docid)
if request.method == 'POST':
form = Appointform(request.POST)
if form.is_valid():
form.save()
f_name = request.POST['f_name']
l_name = request.POST['l_name']
day = request.POST['day']
timeslot = request.POST['timeslot']
email = request.POST['email']
return render(request, 'submit.html', {
'f_name' : f_name,
'l_name' : l_name,
'day' : day,
'timeslot' : timeslot,
'email' : email,
})
form = Appointform()
return render(request, 'takeappointment.html', {'form': form, 'doctor': doctor})
```
how can save the foreign key from Doctor model along with form data in Appoint model?如何保存 Doctor 模型中的外键以及 Appoint 模型中的表单数据?
you can do it this way: add doctor parameter to your Appointform
你可以这样做:将医生参数添加到你的
Appointform
class Appointform(forms.ModelForm):
class Meta:
model = Appoint
def __init__(self, *args, **kwargs):
self.from_doctor = kwargs.pop("from_doctor", None)
super().__init__(*args, **kwargs)
def clean(self):
cleaned_data = super().clean()
cleaned_data["doctor"] = self.from_doctor
return cleaned_data
and in your takeappointment
view add this parameter when create form并在您的
takeappointment
视图中创建表单时添加此参数
def takeappointment(request, docid):
doctor = Doctor.objects.get(docid = docid)
if request.method == 'POST':
form = Appointform(request.POST, from_doctor=doctor)
if form.is_valid():
form.save()
f_name = request.POST['f_name']
l_name = request.POST['l_name']
day = request.POST['day']
timeslot = request.POST['timeslot']
email = request.POST['email']
return render(request, 'submit.html', {
'f_name' : f_name,
'l_name' : l_name,
'day' : day,
'timeslot' : timeslot,
'email' : email,
})
form = Appointform(from_doctor=doctor)
return render(request, 'takeappointment.html', {'form': form, 'doctor': doctor})
you also need to change definition of Appoint model, add blank=True
to your doctor
field您还需要更改 Appoint 模型的定义,将
blank=True
添加到您的doctor
字段
doctor = models.ForeignKey(Doctor, null=True, blank=True, on_delete=models.CASCADE)
you can do it other way.你可以用其他方式来做。
class Appointform(forms.ModelForm):
class Meta:
model = Appoint
exclude = ("doctor", )
def __init__(self, *args, **kwargs):
self.from_doctor = kwargs.pop("from_doctor", None)
super().__init__(*args, **kwargs)
def save(self, *args, **kwargs):
self.instance.doctor = self.from_doctor
return super().save(*args, **kwargs)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.