Check out get_FOO_display, there:
https://docs.djangoproject.com/en/dev/ref/models/instances/
Le 8 août 2011 11:06, "Kayode Odeyemi" <drey...@gmail.com> a écrit :
> In the Django [1], the example as described below shows how to build Forms
> that are tied to a model.
>
> from django.db import models
> from django.forms import ModelForm
>
> TITLE_CHOICES = (
> ('MR', 'Mr.'),
> ('MRS', 'Mrs.'),
> ('MS', 'Ms.'),
> )
>
> class Author(models.Model):
> name = models.CharField(max_length=100)
> title = models.CharField(max_length=3, choices=TITLE_CHOICES)
> birth_date = models.DateField(blank=True, null=True)
>
> def __unicode__(self):
> return self.name
>
> class AuthorForm(ModelForm):
> class Meta:
> model = Author
>
> I have got similar setup in the forms.py and models.py. However, the tuple
> values is not displayed as options in the form field. I have something
like
> this:
>
> models.py
> ---------
>
> """ a tuple of branch code as key and branch name as value """
> MY_INSTITUTION_BRANCHES = (
> ('BR_CODE1', 'HQ'),
> ('BR_CODE2', 'Branch 2'),
> )
>
> class Transaction(models.Model):
> branch_name = models.ForeignKey('MyInstitution',
> related_name='my_institution', \
> help_text='The branch where this transaction is originate from')
>
> class MyInstitution(models.Model):
> name = models.CharField(max_length=100)
> branch_name = models.CharField(max_length=1,unique=True,
> choices=MY_INSTITUTION_BRANCHES)
>
> def __unicode__(self):
> return self.name
>
> forms.py
> --------
> class TransactionUpdateForm(forms.ModelForm):
> class Meta:
> model = Transaction
> fields = ('branch_name')
>
> template file
> -------------
> <label class="label" for="id_branch_name">Branch name: </label>
> {{form.branch_name}}
>
> With this setup, I'm expecting a form select field to be prepopulated with
> the tuple values (human readable names). Instead, the form select field is
> prepopulated with MyInstitution name values as stored in the db.
>
> Any help will be much appreciated.
>
> Thanks
>
> [1]
https://docs.djangoproject.com/en/1.3/topics/forms/modelforms/#modelform
> --
> Odeyemi 'Kayode O.
> http://www.sinati.com. t: @charyorde
>
> --
> You received this message because you are subscribed to the Google Groups
"Django users" group.
> To post to this group, send email to django-users@googlegroups.com.
> To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
> For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to