[Answer]-Django ModelChoiceField show specific field and not the pk field

1👍

Right from the docs:

The unicode method of the model will be called to generate string
representations of the objects for use in the field’s choices; to
provide customized representations, subclass ModelChoiceField and
override label_from_instance. This method will receive a model object,
and should return a string suitable for representing it. For example:

So two options:

# in your model
def __unicode__(self):
    return unicode(self.uuid)

or, much better if you need to keep a different string representation:

from django import forms

class UUIDChoiceField(forms.ModelChoiceField):
    def label_from_instance(self, obj):
        return unicode(obj.uuid)

class FormWithUUIDChoiceField(forms.form):
    field1 = UUIDChoiceField(queryset=..., ...)

Leave a comment