我需要一个好的提示,我如何为这两个模型创建注册表:
class Adress(models.Model):
"""
Abstract model for users and organizations adresses
"""
country = models.CharField(max_length=50)
city = models.CharField(max_length=50)
street = models.CharField(max_length=50)
house_number = models.IntegerField()
postal_code = models.CharField(max_length=50)
class Meta:
verbose_name = "Adress"
verbose_name_plural = "Adresses"
And Patient model:
class Patient(DiaScreenUser):
"""
Choices for diabet type options
"""
FIRST_TYPE = '1'
SECOND_TYPE = '2'
NULL_TYPE = 'null'
DIABETES_TYPE_CHOICES = (
(FIRST_TYPE, '1 тип'),
(SECOND_TYPE, '2 тип'),
(NULL_TYPE, 'відсутній'),
)
height = models.DecimalField(max_digits=6, decimal_places=2)
weight = models.DecimalField(max_digits=6, decimal_places=2)
body_mass_index = models.DecimalField(max_digits=6, decimal_places=2, blank=True, null=True)
connect_to_doctor_date = models.DateTimeField(blank=True)
diabet_type = models.CharField(max_length=10, choices=DIABETES_TYPE_CHOICES, default=NULL_TYPE)
is_oninsuline = models.BooleanField(db_index=True)
doctor_id = models.ForeignKey(Doctor, on_delete=models.SET_NULL, blank=True, null=True)
adress_id = models.ForeignKey(Adress, on_delete=models.SET_NULL, blank=True, db_index=True, null=True)
我需要一份注册表,患者可以在其中输入他的个人信息和地址信息(但当我的患者模型中有外键时,我无法理解它是如何工作的)。感谢您的任何建议!
我尝试创建类似的东西,但在这种情况下我无法理解如何将地址链接到患者
class AdressForm(ModelForm):
class Meta:
model = Adress
fields = ["country","city"]
class PatientForm(ModelForm):
adress_form = AdressForm()
class Meta:
model = Patient
fields = [
"username",
"email",
"phone_number",
]
这里有一些使用基于类的视图的提示。总体思路是:为 Patient 创建一个表单,其中包含一些与 Adress 相对应的额外字段,然后在视图中从此表单创建两个对象。
1。表格
通过指定更多表单字段来增强您的 PatientForm。您希望 Adress 模型的每个必填字段都有一个表单字段。您可以指定诸如发送表单所需的字段、字符串应该多长之类的内容...检查文档
from django import forms
from ..models import Patient
class PatientForm(forms.ModelForm) :
class Meta:
model = Patient
fields = [
"username",
"email",
"phone_number",
# any other field you need from Patient model
]
# then add the fields you need to create Adress model
country = forms.CharField(label="Country", required=False)
city = forms.CharField(label="City", required=False)
street = forms.CharField(max_length=50)
house_number = forms.IntegerField()
postal_code = forms.CharField()
2。景色
您的表单与一个视图配对,您可以在其中创建地址,将其保存在数据库中,然后使用它来创建您的患者:
from django.shortcuts import redirect
from django.urls import reverse_lazy
from django.views.generic import CreateView
from ..forms import PatientForm
from ..models import Patient, Adress
class PatientFormView(CreateView):
model = Patient
form_class = PatientForm
success_url = reverse_lazy("home")
def form_valid(self, form):
adress = Adress(
country = form.cleaned_data["country"],
city = form.cleaned_data["city"],
street = form.cleaned_data["street"],
house_number = form.cleaned_data["house_number"],
postal_code = form.cleaned_data["postal_code"]
)
adress.save()
self.object = Patient(
username = form.cleaned_data["username"],
email = form.cleaned_data["email"],
phone_number = form.cleaned_data["phone_number"]
...
adress = adress
)
self.object.save()
return redirect(self.get_success_url())