I want to populate two foreign key fields in one of my forms. The relevant bit of code is as below:
if request.method == 'POST':
form = IssuesForm(request.POST or None)
if request.method == 'POST' and form.is_valid():
form.save()
else:
form = IssuesForm(initial={'vehicle': stock_number, 'addedBy': request.user, })
vehicle
points to the Vehicle
class. addedBy
is to contain the currently logged in user.
However the drop downs aren't initialized as I want...I still have to select the vehicle and user. From this I have two questions:
- What could be the problem?
- What is the best way to make these forms read-only?
EDIT 1
The IssueForm
class looks like this so far:
class Issues(models.Model):
vehicle = models.ForeignKey(Vehicle)
description = models.CharField('Issue Description', max_length=30,)
type = models.CharField(max_length=10, default='Other', choices=ISSUE_CHOICES)
status = models.CharField(max_length=12, default='Pending',
choices=ISSUE_STATUS_CHOICES)
priority = models.IntegerField(default='8', editable=False)
addedBy = models.ForeignKey(User, related_name='added_by')
assignedTo = models.CharField(max_length=30, default='Unassigned')
dateTimeAdded = models.DateTimeField('Added On', default=datetime.today,
editable=False)
def __unicode__(self):
return self.description
Form Class
class IssuesForm(ModelForm):
class Meta:
model = Issues
exclude = ('assignedTo')