在CreateView中自动填写字段

时间:2017-01-10 10:06:22

标签: python html django templates

我有模特:

class Patient(models.Model):
patientID = models.CharField(max_length=200 , help_text='Insert PatientID')
birth_date = models.DateField(auto_now=False, auto_now_add=False, help_text='YYYY-MM-DD')
gender = models.CharField(max_length=200,choices=Gender_Choice, default='UNDEFINED')

class GeneralData(models.Model):
patient = models.ForeignKey(Patient, on_delete=models.CASCADE)
examination = models.CharField(max_length=100, choices=Examination_Choice, default='notchosenyet')
height = models.FloatField(help_text= '[m] not [cm]! ')
weight = models.FloatField(help_text= '[kg]')

在我的网站中,我在将患者添加到患者详细信息页面后导航用户。在那里,用户可以通过添加按钮添加GeneralData。现在我想要的是,GeneralData的CreateView会自动选择患者。因为用户从一个特定患者的患者详细信息页面进入CreateView,我认为这应该是可能的。但我对Django和Web开发一般都是新手,并且不知道如何解决我的问题。

感谢您的帮助!

P.S。对不起我的英语不好。

编辑:

我的网址:

# /member/<patient_id>/
url(r'^(?P<pk>[0-9]+)/$', views.DetailView.as_view(), name='detail'),

# /member/generaldata/add/

url(r'patient/(?P<pk>[0-9]+)/generaldata/add/$', views.GeneralDataCreate.as_view(), name='generaldata-add'),

我的观点:

class GeneralDataCreate(generic.CreateView):
model = GeneralData
fields = ['patient', 'examination', 'height', 'weight', 'aha_classification']

   def get_initial(self):
   patient = get_object_or_404(Patient.objects.get(pk=self.kwargs.get('pk')))
   return {
    'patientID': patient,
    }

0 个答案:

没有答案
相关问题