[英]Django saving data in model through model form and foreign key
我有一個名為Doctor的模型和一個名為Appoint的模型,其中有醫生作為外鍵。 並且我有一個模型表單來為 Appoint 模型輸入輸入,但不能輸入其中的外鍵。 我可以通過 url 鏈接 Doctor 模型,但我無法在 Appoint 模型中保存醫生(外鍵)。
這是 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```
這是查看方法:-
``` 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})
```
如何保存 Doctor 模型中的外鍵以及 Appoint 模型中的表單數據?
你可以這樣做:將醫生參數添加到你的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
並在您的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})
您還需要更改 Appoint 模型的定義,將blank=True
添加到您的doctor
字段
doctor = models.ForeignKey(Doctor, null=True, blank=True, on_delete=models.CASCADE)
你可以用其他方式來做。
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.