How do I specify (1) an order and (2) a meaninful string representation for users in my Django application?

Posted by David Faux on Stack Overflow See other posts from Stack Overflow or by David Faux
Published on 2012-06-02T16:37:12Z Indexed on 2012/06/02 16:41 UTC
Read the original article Hit count: 210

Filed under:

I have a Django application with users. I have a model called "Course" with a foreign key called "teacher" to the default User model that Django provides:

class Course(models.Model):
    ...
    teacher = models.ForeignKey(User, related_name='courses_taught')

When I create a model form to edit information for individual courses, the possible users for the teacher field appear in this long select menu of user names.

enter image description here

These users are ordered by ID, which is of meager use to me. How can I

  1. order these users by their last names?
  2. change the string representation of the User class to be "Firstname Lastname (username)" instead of "username"?

© Stack Overflow or respective owner

Related posts about django