The label_from_instance should simply return the obj. I don't think any
fuzzy codes is needed as
described in the Django docs [1]

The method label_from_instance already converts the objects to string. So a
simple override like this:

class MyModelChoiceField(ModelChoiceField):
    def label_from_instance(self, obj):
        return obj

I made changes like this to my code:

""" Introduced a new model to store the form field select values """
class Branch(models.Model):
    """Branch"""
    name = models.CharField(max_length=50)
    name_branch = models.CharField(max_length=50)
    address_1 = models.CharField(max_length=100)

    class Meta:
        db_table = 'branch'

    def __unicode__(self):
        return u"%s | %s | %s" % (
            unicode(self.name_branch),
            unicode(self.address_1))

""" subclass of ModelChoiceField """"
class BranchModelChoiceField(ModelChoiceField):
    def label_from_instance(self, obj):
        return obj # Just return the obj

class TransactionUpdateForm(forms.ModelForm):
    branchqs =Branch.objects.values_list('name_branch', flat=True)
    branches_field = BranchModelChoiceField(branchqs) #ModelChoiceField
instance
    class Meta:
        model = Transaction
        fields = ('branch_name')

In template file I do this:

{{form.branches_field}}

Thanks everyone

[1] https://docs.djangoproject.com/en/1.3/ref/forms/fields/#modelchoicefield

On Mon, Aug 8, 2011 at 8:54 PM, Kayode Odeyemi <drey...@gmail.com> wrote:

> I have made changes like this:
>
> """ Introduced a new model to store the form field select values """
> class Branch(models.Model):
>     """Branch"""
>     name = models.CharField(max_length=50)
>     name_branch = models.CharField(max_length=50)
>     address_1 = models.CharField(max_length=100)
>
>     class Meta:
>         db_table = 'branch'
>
>     def __unicode__(self):
>         return u"%s | %s | %s" % (
>             unicode(self.name_branch),
>             unicode(self.address_1))
>
> class TransactionUpdateForm(forms.ModelForm):
>     branchqs = Branch.objects.get(name_branch)
>     branches_field = forms.BranchModelChoiceField(branchqs)
> #ModelChoiceField instance
>     branch_name_new = forms.models.CharField(required=True,
> widget=forms.Select(choices={'BR_CODE1': 'HQ': 'Branch 2'}))
>      class Meta:
>         model = Transaction
>         fields = ('branch_name')
>         widgets = {
>             'branch_name_new': Select(choices={'BR_CODE1': 'HQ': 'Branch
> 2'}),
>         }
>
> """ subclass of ModelChoiceField """"
> class BranchModelChoiceField(ModelChoiceField):
>     def label_from_instance(self, obj):
>         return "My Object #%i" % obj.id
>
> In template file I do this:
>
> {{form.branches_field}}
>
> Unfortunately, no luck yet.
>
> I'm still trying other options though.
>
> Thanks
>
> On Mon, Aug 8, 2011 at 3:20 PM, Tom Evans <tevans...@googlemail.com>wrote:
>
>> On Mon, Aug 8, 2011 at 2:56 PM, Daniel Roseman <dan...@roseman.org.uk>
>> wrote:
>> >
>> > I'm afraid it's really not clear what you're trying to do, or what the
>> > problem is.
>> > If the issue is that you have a field referring to a foreign key, and
>> you
>> > want to change what the values displayed in the dropdown for that field,
>> > then you simply need to redefine the `__unicode__` method for the
>> related
>> > model, as documented
>> > here:
>> https://docs.djangoproject.com/en/1.3/ref/forms/fields/#modelchoicefield
>> > An alternative, also documented at that link, is to subclass
>> > ModelChoiceField and override label_from_instance.
>>
>> This exact question gets asked about 3 times a week (well, once you
>> have normalized out the different ways in which it is asked..).
>> Personally, I'm fed up of answering it.
>>
>> I think that adjusting the labels needs to become a bit more flexible
>> - perhaps allow a "label_from_instance=callable" argument on
>> ModelChoiceField, or have ModelChoiceField look for a
>> MyForm.label_from_instance_<fieldname> method.
>>
>> The current options are
>>
>> a) change how the related field's model is displayed throughout Django
>> (which may be impossible to do for a cross app links/3rd party apps)
>> b) provide your own Field class which labels things appropriately
>>
>> Both these options are more cumbersome than needs be, as evidenced by
>> the number of users who don't understand how to do this.
>>
>> Cheers
>>
>> Tom
>>
>> --
>> 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
>
>


-- 
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.

Reply via email to