On Mon, Oct 24, 2011 at 4:34 PM, Tom Evans <[email protected]> wrote:
> On Mon, Oct 24, 2011 at 4:21 PM, Kayode Odeyemi <[email protected]> wrote: > > On Mon, Oct 24, 2011 at 2:57 PM, Tom Evans <[email protected]> > wrote: > >> > >> On Mon, Oct 24, 2011 at 11:14 AM, Kayode Odeyemi <[email protected]> > >> wrote: > >> > Hello all, > >> > > >> > class BranchModelChoiceField(ModelChoiceField): > >> > def label_from_instance(self, obj): > >> > return obj > >> > > >> > class TransactionUpdateForm(forms.ModelForm): > >> > branchqs = Branch.objects.values_list(u'name_branch', flat=True) # > >> > use > >> > flat=True if values_list is called with a single field > >> > branch_name = BranchModelChoiceField(branchqs) > >> > class Meta: > >> > model = Transaction > >> > fields = ('teller_no', 'paid') > >> > >> > >> Two things here. ModelChoiceField wants a real queryset, not a > >> ValuesQuerySet. The values of the model choice field should be the > >> primary keys of the model being chosen, which is not the case here > >> (which is why it fails). > >> > >> The second thing is that queryset is evaluated precisely once, when > >> the form is instantiated for the first time. When you add a branch, it > >> will not appear in that form until the server is restarted. > >> > >> Judging from your other reply, you don't actually want a foreign key > >> link to the branch, you simply want to store the name of the branch on > >> the transaction? If so, you should simply declare a ChoiceField and > >> supply it with choices derived from Branch. > >> > >> Eg: > >> > >> class TransactionUpdateForm(forms.ModelForm): > >> branch_name = ChoiceField(required=True) > >> def __init__(self, *args, **kwargs): > >> super(TransactionUpdateForm, self).__init__(*args, **kwargs) > >> self.fields['branch_name'].choices = > >> Branch.objects.all().values('name_branch', 'name_branch') > >> > > Thanks for writing in Tom. > > > > I think what you did is the same as: > > > > class TransactionUpdateForm(forms.ModelForm): > > branchqs = Branch.objects.values_list('bid', u'name_branch') > > branch_name = BranchModelChoiceField(branchqs) > > No! It really isn't. > > 1) In my one the queryset is created inside the form's __init__ > method, so it gets executed correctly every time the form is > instantiated. In yours, the queryset is only evaluated once, so has > incorrect values as soon as a branch is added or deleted. > 2) If you are using a ModelChoiceField, give it a QuerySet, not a > ValuesListQuerySet - really! This is why you get your unpacking > errors. However, don't you want to store the name of the branch on the > transaction, not the id of the branch? If so, you want a Choice field, > since you are not linking to another object, just denormalizing some > data from it into the Transaction model. > > > > > I did try this earlier as well but didn't go to far with it. > > > > But then in the template file, I have {{form.branch_name}} which results > > into "Need more than 1 value to unpack error". How am I expected to print > > the form > > widget (select field) in a template? > > > > Assuming you don't give a ModelChoiceField an incorrect value for it's > queryset, it is just this: > > {{ form.name_of_field }} > > I'm sorry but it doesn't just seem to work for me. I have tried possibilities like: self.fields['branch_name'].choices = Branch.objects.all().values('name_branch', 'name_branch') self.fields['branch_name'].choices = Branch.objects.all().values('name_branch') in template ---------------- {{form.branch_name}} and <select name="branch_name" class="form-select " id="id_branch_name" > {% for choice in form.branch_name.field.choices %} {% choice.name_branch %} <option value="0">{% choice.name_branch %}</option> {% endfor %} </select> Both reporting "Need more than 1 value to unpack error". Does this have anything to do with if perhaps the branch_name db field contains NULL. >From the interactive shell, I get more that 1 result. Thanks -- 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 [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

