简体   繁体   中英

Making a list off all users in Django

I am trying to create a list of all the users on my Django setup in order to list who is working on a device, currently it pulls the usernames.

REPAIRED_BY = (
    ('BLANK', 'Not Assigned'),
    ('USER1', User.objects.all()),
)

but it does not allow me to list them in a string format (ie Joe Smith). I am able to get the first and last name using value_list but it appears as a tuple. ('Joe','Smith')

repaired_by = models.CharField(max_length=100, choices=REPAIRED_BY, default='BLANK')

It needs to be able to switch users to different devices. User 1 is working on computers 1 & 2 while user 2 is working on computer 3

Please let me know if you need more information.

It seems that you would want to use a ForeignKey field, to bind a User to a Device.

https://docs.djangoproject.com/en/1.8/ref/models/fields/#django.db.models.ForeignKey

repaired_by = models.ForeignKey(User, null=True)

in order to list [...] does not allow me to list them in a string format (ie Joe Smith). I am able to get the first and last name using value_list but it appears as a tuple. ('Joe','Smith')

I'm not sure I get your issue right, but getting a convenient name display from a tuple of name parts is trivial:

t = ('Joe','Smith') 
print(" ".join(t))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM