Edit widgets Django

Asked

Viewed 121 times

3

Hello! I’m developing a Django app. On the project registration page appears a Selectmultiple pro user select the participating teachers. But on the details page I just wanted to appear the names of the teachers who were selected there in the register. I put instead of Selectmultiple the Textinput widget, but instead of appearing the teacher’s name, only the ID appears. I wanted the name to appear. How do I?

  • Have you tried the ModelChoiceField? See also that answer in Soen, it shows an example of how to use the Select with a list of objects in a model (a SelectMultiple should work the same, since he inherits from Select).

1 answer

3

to get what you want, go to the model Professor and implement the method __str__ or __unicode__. Something like:

class Professor(Model):
    #...
    def __str__(self):
        return self.nome #assumindo que nome é o atributo do nome do professor.

    def __unicode__(self):
        return self.nome

The method __str__ indicates how an object of that class will be displayed when prompted for its string and method __unicode__ sets how an object of that class will be displayed when prompted to your Unicode.

  • 2

    The method __unicode__ only required if using Python 2, as 3 uses Unicode strings by default (but is more "guaranteed" to have both, and can also call each other).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.