That didn't help either. It still did not depict what I'm trying to do. Those filters are for retrieving values set by a user. However, I tried the code below which is to hard code the choices into the form field on creation
class TransactionUpdateForm(forms.ModelForm): branch_name_new = forms.models.CharField(required=True, widget=forms.Select(choices={'BR_CODE1': 'HQ','BR_CODE2': 'Branch 2'})) class Meta: model = Transaction fields = ('branch_name') widgets = { 'branch_name_new': Select(choices={'BR_CODE1': 'HQ': 'Branch 2'}), } In the template, I did an output like this: {{form.branch_name_new}}. Nothing gets displayed. How does Django handle this literally. I have read the docs insideout and tried stuffs out as written, but nothing seem to work. Has anyone done this sort of stuff before - I mean this is meant to be basic stuff. I don't want to have to manually create HTML markups in the template, because I will be needing those Select field values coming from the db. Thanks On Mon, Aug 8, 2011 at 11:00 AM, Thomas Orozco <g.orozco.tho...@gmail.com>wrote: > Oh, sorry, I must have misunderstood then. > > I remember reading a post on thread on SO that addressed this, and I > believe that they came up with a template filter as there was no "simple" > way to do it. > > Here it is: > http://stackoverflow.com/questions/1105638/django-templates-verbose-version-of-a-choice > > Hope this helps! > Le 8 août 2011 11:56, "Kayode Odeyemi" <drey...@gmail.com> a écrit : > > > Thanks for the reply. > > > > I have a views.py like this: > > > > @login_required > > def update_transaction(request, tpin=None): > > qs = Q(my_institution=None) | > > Q(my_institution=request.user.get_profile().my_institution) > > txn = get_object_or_404(Transaction.objects.filter(qs), tpin=tpin) > > context = {'page_title': 'Update Transactions'} > > if request.POST: > > update_form = TransactionUpdateForm(request.POST, instance=txn) > > if update_form.is_valid(): > > update_form.save() > > return HttpResponseRedirect(reverse('search-txn')) > > > > else: > > update_form = TransactionUpdateForm(instance=txn) > > context['txn'] = txn > > context['form'] = update_form > > return render_to_response("update_transaction.html", context, > > context_instance=RC(request)) > > > > At what point do I do: update_form.get_branch_name_display()? In forms.py > or > > views.py? Where? > > > > It seems to me that get_FOO_display() is meant to display the values set > by > > the user. This is not what I'm looking to do. What I got stuck with is > that > > the tuple values are not displayed as options in the form select > > field(choices). > > > > Thanks > > > > On Mon, Aug 8, 2011 at 10:40 AM, Thomas Orozco < > g.orozco.tho...@gmail.com>wrote: > > > >> 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. > >> > > > > > > > > -- > > 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. > -- 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.