Reputation: 5863
I have a model like this:
class Appointment(models.Model):
user = models.ForeignKey(User)
engineer = models.ForeignKey(Engineer)
first_name = models.CharField(max_length=100)
middle_name = models.CharField(max_length=100, blank=True)
last_name = models.CharField(max_length=100)
age = models.IntegerField()
def __unicode__(self):
return self.first_name
I have created a form using this model and I want to save it. Here I want the 'engineer' field to to be as the primary key is passed in the url.. like
engineer = Engineer.objects.get(pk=pk)
How can I do this. Or I should create a normal form and get its value via get method and assign to the field??
Upvotes: 3
Views: 3148
Reputation: 599758
Since this is a CreateView, you should build on the example in the documentation and add it in form_valid
:
def form_valid(self, form):
form.instance.engineer = Engineer.objects.get(pk=self.kwargs['pk'])
return super(AppointmentCreate, self).form_valid(form)
Upvotes: 4