2016-12-01 2 views
0

모델 '약속'이 있습니다. 그 클래스 환자는 내장 된 사용자 모델과 연관되어 있습니다. 나는 그 클래스에 대한 모델 형식을 만들었습니다. 동일한 폼 내에서 다른 클래스 (필드 환자의 사용자)의 개체를 인스턴스화 할 수 있습니까? 양식의 사용자 모델 필드에 액세스 할 수 없습니다. 어떻게 할 수 있습니까? 나는 다음과 같이이 모델에 대한 ModelForm을 만든하위 클래스의 ModelForm에서 상위 클래스 개체 만들기

class Appointment(models.Model): 
    doctor = models.ForeignKey(Doctor) 
    clinic = models.ForeignKey(Clinic, null=True, blank=True) 
    hospital = models.ForeignKey(Hospital, null=True, blank=True) 
    day = models.DateField() 
    time = models.TimeField() 
    patient = models.ForeignKey(User) 

:

class HospitalCreateAppointmentForm(forms.ModelForm): 
    def __init__(self, *args, **kwargs): 
     super(HospitalCreateAppointmentForm, self).__init__(*args, **kwargs) 
     for field in iter(self.fields): 
       self.fields[field].widget.attrs.update({ 
        'class': 'form-control' 
       }) 

    class Meta: 
     model = Appointment 
     fields = ['doctor','hospital','day','time','patient'] 
     widgets = { 
      'day': forms.SelectDateWidget(), 
     } 

답변

1

나는 HospitalCreateAppointmentForm에서 사용자 개체를 인스턴스화 뒤에 확인 목적 아니다. 그러나 필요한 경우에 대비하여 사용자 파일을 양식 파일로 가져와야합니다. 예를 들어 당신의 HospitalCreateAppointmentFor

from django.contrib.auth.models import User 

같은 수입 사용자 모듈, forms.py에있는 경우 그리고 당신은 같은 필드에 액세스 할 수 있습니다

User._meta.fields 
+0

약속을 예약 할 때마다, 나는 것을 원하는이 그것을 인스턴스화 뒤에 추론 환자가 사이트에서 사용자가되어 자신의 약속 상태를 확인할 수 있습니다. – blueChair

관련 문제