Reputation: 6077
I am trying to create a form which is linked with a ForeignKey.
Model.py
enter code here
model.py
class CreateD(models.Model):
id=models.IntegerField(primary_key=True)
db = models.CharField(max_length=50, blank=True)
iss = models.CharField(max_length=100, blank=True)
status = models.CharField(max_length=2, blank=True ,default='A')
class Meta:
db_table = u'add_d'
def __unicode__(self):
return u'%s %s' % (self.db,self.iss)
class Assignment(models.Model):
id = models.IntegerField(primary_key=True)
assign_to = models.CharField(max_length=50, blank=True)
db_name = models.ForeignKey(CreateDb,related_name='set__dbname')
issue_type = models.ForeignKey(CreateDb,related_name='set_issuetype')
date = models.DateTimeField(null=True, blank=True)
class Meta:
db_table = u'assignment'
def __unicode__(self):
return u'%s %s' % (self.db_name,self.issue_type)
form.py
class AssignmentForm(ModelForm):
class Meta:
model = Assignment
exclude = ('id','date','assign_to')
assign.html
{{forms.as_p}}
Data in the sql :
id db iss
1 A AB
2 B BA
Output genrated from html page is
db_name : A AB (choice field)
B BA
issue_type : A AB (choice field)
B BA
Actually i Need a output like below in html:
db_name : A (choice field)
B
issue_type : AB (choice field)
BA
Please help me on this ...........
Upvotes: 0
Views: 322
Reputation: 50806
If I get your question right you want to change the labels in the select box, by default they are generated via the model's __unicode__
method, but you can override the form field's label_from_instance
method and choose another way to do this; see Django forms: how to dynamically create ModelChoiceField labels!
Upvotes: 1