在django中插入一对一的字段值 [英] Insert One to One field value in django

查看:145
本文介绍了在django中插入一对一的字段值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  class PatientInfo(models.Model):
lastname = models.CharField ('Last Name',max_length = 200)
firstname = models.CharField('First Name',max_length = 200)
middlename = models.CharField('Middle Name',max_length = 200)
...
def get_absolute_url(self):
return reverse('patient:medical-add',kwargs = {'pk':self.pk})


类MedicalHistory(models.Model):
patient = models.OneToOneField(PatientInfo,on_delete = models.CASCADE,primary_key = True,)
...

,并且在提交PatientInfo表格时它将转到提供MedicalHistory Details的另一个表格。我可以看到我的PatientInfo数据以及MedicalHistory数据,但没有相互关联。以下是我的MedicalCreateView,它处理我的MedicalHistory表单。

  class MedicalCreateView(CreateView):

template_name = 'patient / medical_create.html'
model = MedicalHistory
form_class = MedicalForm

def post(self,request,pk):
form = self.form_class(request .POST)


if form.is_valid():
patiente = form.save(commit = False)
physician_name = form.cleaned_data ['physician_name'] #不要删除
patient = PatientInfo.objects.all(id = self.kwargs ['pk'])

MedicalHistory.patient = self.kwargs ['pk']


patiente.save()
messages.success(请求,%s被添加到患者列表中%physician_name)

返回重定向('index' )
else:
print(form.errors)

这就是我设置MedicalHistor y.patient字段使用PatientInfo.pk

  MedicalHistory.patient = self.kwargs ['pk'] 


解决方案

如果您使用 OneToOneField 并且想要自动连接 MedicalHistory PatientInfo ,您需要使用信号 b

$ p $ class MedicalHistory(models.Model):
patient = models.OneToOneField(PatientInfo,on_delete = models.CASCADE,primary_key = True,)
。 。 。 。 。

@receiver(post_save,sender = PatientInfo)
def create_medical_history(发件人,实例,创建,** kwargs):
如果创建:
MedicalHistory.objects。 create(patient = instance)

@receiver(post_save,sender = PatientInfo)
def save_medical_history(sender,instance,** kwargs):
instance.medicalhistory.save()

浏览

  class MedicalCreateView(CreateView):

template_name ='patient / medical_create.html'
model = MedicalHistory
form_class = MedicalForm
success_url ='/'


I have the following models.

class PatientInfo(models.Model):
    lastname = models.CharField('Last Name', max_length=200)
    firstname = models.CharField('First Name',max_length=200)
    middlename = models.CharField('Middle Name',max_length=200)
    ...
    def get_absolute_url(self):
       return reverse('patient:medical-add', kwargs={'pk': self.pk})


class MedicalHistory(models.Model):
    patient = models.OneToOneField(PatientInfo, on_delete=models.CASCADE, primary_key=True,)
    ...

and upon submitting PatientInfo form it will go to another form which supply the MedicalHistory Details. I can see my PatientInfo data as well as MedicalHistory data but not linked to each other. Below is my MedicalCreateView which process my MedicalHistory form.

class MedicalCreateView(CreateView):

    template_name = 'patient/medical_create.html'
    model = MedicalHistory
    form_class = MedicalForm

    def post(self, request, pk):
        form = self.form_class(request.POST)


        if form.is_valid():
            patiente = form.save(commit=False)
            physician_name = form.cleaned_data['physician_name'] # do not delete
            patient = PatientInfo.objects.all(id=self.kwargs['pk'])

            MedicalHistory.patient = self.kwargs['pk']


            patiente.save()
            messages.success(request, "%s is added to patient list" % physician_name )

            return redirect('index')
        else:
            print(form.errors)

This is how I set MedicalHistory.patient field using the PatientInfo.pk

MedicalHistory.patient = self.kwargs['pk']

解决方案

If you are using OneToOneField and want to link MedicalHistory to PatientInfo automatically you need to use signals.

class MedicalHistory(models.Model):
    patient = models.OneToOneField(PatientInfo, on_delete=models.CASCADE, primary_key=True,)
    . . . . . 

@receiver(post_save, sender=PatientInfo)
def create_medical_history(sender, instance, created, **kwargs):
    if created:
        MedicalHistory.objects.create(patient=instance)

@receiver(post_save, sender=PatientInfo)
def save_medical_history(sender, instance, **kwargs):
    instance.medicalhistory.save()

Views

class MedicalCreateView(CreateView):

    template_name = 'patient/medical_create.html'
    model = MedicalHistory
    form_class = MedicalForm
    success_url = '/'

这篇关于在django中插入一对一的字段值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆