class Task(models.Model):
owner_set=set()
for each in User.objects.all():
owner_set.add((each.username,each.username))
Owner = models.CharField(max_length=100,choices=owner_set)
In the code, i create a field by the choices of User, but this leads a problem:
When i create a new user, the new username will not apprear in the filed of “Owner”;
i need to restart the server. So i want that the Owner filed should be dynamic, is there any way to do this?
I don’t want to use the Owner= models.ForeignKey(User,...) because may be i will use the names not only from the auth_user.
Why would you store Owner as a CharField? If it’s a reference to a User object, it should be a ForeignKey.
Edit after comment Your data model seems broken. Nevertheless, if you really want to keep it as a CharField, you should not try to set the choices in the model at all. Define a custom form and either use a ModelChoiceField or set the choices in the form’s
__init__method.