Reputation: 155
I have a Model 'Appointment'. In that class patient is associated with built-in User model.I have made a modelform for that class. Is it possible to instantiate an object of another class (User in field patient) from inside the same form. I am not able to access User Model's field in the form. How can this be done ?
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)
I have created a ModelForm for this model as below:
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(),
}
Upvotes: 0
Views: 256
Reputation: 2945
I am not sure purpose behind instantiating user object in HospitalCreateAppointmentForm
. However, just in case you really need, you have to import User model in form file. For example if your HospitalCreateAppointmentFor
is in forms.py
, import User module like
from django.contrib.auth.models import User
And you can access field like:
User._meta.fields
Upvotes: 1