Search code examples
pythondjangodjango-modelsrepresentationgeneric-foreign-key

django foreignkey - format output value in selectlist


A form generates a list of all available foreign key elements. They are displayed depending on the __str__ function defined in the model.

I don't know how to search for this, but need to change this without touching the model.

My model connects with the User model. This model shows the username, but i would like to call the _get_full_name function to get a better list.

class Trainer(models.Model):
    user=models.OneToOneField(User)

Is it possible to define it inside the definition of the Foreignkey or inside the model which connects (in my example the Trainer model)?


Solution

  • This should work:

    def user_new_unicode(self):
        return self.get_full_name()
    User.__unicode__ = user_new_unicode #or User.__str__ = user_new_unicode
    

    Of course it should be placed in your class.