2

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 (i.e. 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.

2 Answers2

2

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)
user1797792
  • 1,169
  • 10
  • 26
  • That almost works but it removed my list. The plan/idea is to allow a dropdown menu to assign different users to the device (i.e. Joe Smith is working on Computer 1 while John Smith is working on Computer 2). I'll add that for more information – Daniel Sapelli Oct 27 '15 at 14:30
  • Thats what ForeignKey is intented for. If you look in your django-admin, you will see a dropdown with all your Users – user1797792 Oct 27 '15 at 14:33
  • 1
    The docs for [ModelForm FieldTypes](https://docs.djangoproject.com/en/1.8/topics/forms/modelforms/#field-types) state that ForeignKey uses a ModelChoiceField.. which would give the dropdown list. – Sayse Oct 27 '15 at 14:35
  • I added the `null=true` like suggested and that did fix the issue, however, it's pull them up as user names, I am trying to pull first and last name so it shows up as Joe Smith instead of jsmith – Daniel Sapelli Oct 27 '15 at 14:38
  • 1
    For that you would have to change the *__unicode__* method of django auth User class. Some ideas are presented [here](http://stackoverflow.com/questions/5062493/override-django-user-model-unicode) – Lucas Infante Oct 27 '15 at 15:11
  • 1
    I managed to figure it out [here](http://stackoverflow.com/questions/3966483/django-show-get-full-name-instead-or-username-in-model-form) Thanks to @maccinza for the suggestion to override the unicode and OP for the answer to getting my user information. – Daniel Sapelli Oct 27 '15 at 16:48
-1

in order to list [...] does not allow me to list them in a string format (i.e. 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))
decltype_auto
  • 1,706
  • 10
  • 19
  • Thanks for the suggestion, but unfortunately that does not help enough. I need a drop down menu, (which the first suggestion helped with). – Daniel Sapelli Oct 27 '15 at 14:40