Django 5.1.1
Python 3.12.4
I need to get all the Django username’s and use them as a choice field in model . I generally use
user = request.user to get a user’s information but I need all users and use them in a choice class
class ManagerMessages(models.Model):
class UserChoices(models.TextChoices):
*Not sure what to put here*
UserName= models.CharField(max_length=10, choices=UserChoices.choices)
Actually, the appropriate option here would be to change that field to be a ForeignKey to your User model.
Quoting directly from the Django docs for choices
at: Model field reference | Django documentation | Django
Note that choices can be any sequence object – not necessarily a list or tuple. This lets you construct choices dynamically. But if you find yourself hacking choices
to be dynamic, you’re probably better off using a proper database table with a ForeignKey
. choices
is meant for static data that doesn’t change much, if ever.