Make a model field choose from a database of users - django

So, I'm making an item checkout app for our internal group. So, my django app has a model of an item, with one of the fields being loanedTo.
We have a database of users in the building, and I would like to set the choices field of this loanedTo field to take values from the db. So, when someone checks out an item, he can select from a list of users.
I can't seem to find how to do this exactly?
On here, https://docs.djangoproject.com/en/2.0/ref/models/fields/, the choices field can be set to an iterable list or tuple.

Related

Confusion in id parameter in django

I was making an E Commerce website and added various products in it. Now for making a cart i made a add to cart button. And in HTML i assigned an id to this button. The id was pr{{product.id}}. I have not made buttons individually for every item. There is a for loop running which creates buttons. Now My doubt is that i have not assigned any field id in the models class. But still this code is running and when i printed the id's on the console i realised that it prints like pr32, pr33, pr34 ie in a sequential manner. So is there any product.id field predefined in django??
My doubt is that I have not assigned any field id in the models class.
You don't need to. If you do not add a primary key yourself, Django will add a field with the name id that is an AutoField. This is documented in the Automatic primary key fields section of the documentation:
By default, Django gives each model the following field:
id = models.AutoField(primary_key=True)
This is an auto-incrementing primary key.
If you’d like to specify a custom primary key, specify
primary_key=True on one of your fields. If Django sees you’ve
explicitly set Field.primary_key, it won’t add the automatic id
column.
Each model requires exactly one field to have primary_key=True
(either explicitly declared or automatically added).
So to answer your question:
So is there any product.id field predefined in django?
Yes, if you did not specify a field with primary_key=True yourself.

How to get list of all fields corresponding to a model saved in content type framework in django

I have this requirement in which I have to show two dropdown fields on Django admin page.
The first field will be the dropdown of all the table names available in my project, and the second field will be a dropdown all the available fields of the table selected in 1st dropdown. The second field will be dynamic based on the selection of the first dropdown. I am planning to handle this by overriding change_form_template.
Now, I can show the dropdown of table names by fetching them through content type Django model, But not able to fetch corresponding fields as content type model save the model name as a string. So is there any way not necessarily using Content-Type to achieve such a requirement?
Any help around that will be highly appreciated.
Thank you.
Here you have dependency of another fields right so i think you need to see i give link,then yo get error let me know
link:hint of dependency field in django

Additional field on django rest framework filters and form (not stored)

There are 2 scenarios here where I'm looking to add an additional non-stored field to the DRF 3.0 generics views. On both my add contact and list contacts web services I was able to call these correctly using python as client. I would like to be able to the same functionality possible in the browser though as well.
For add contact I need to add a scenario ('phone', 'form') so I can have different validations take placed based on this value.
For the list contacts, I have an interesting data scenario where I want to check 2 fields and related table for the phone number...so on this I'd just like to be able to add a field with name search_phone to the filters on the list view so I can handle it in get_queryset.
I tried adding a field to ModelSerializer but I couldn't get it in the form or filter.
TIA

Django ModelForm with Many to Many Field

I have a Django model for a List with a many to many field to Item, it uses a through table, that has a column for quantity.
I want to use Django forms to display a form such that the user can add items to the list. If I just try and create the ModelForm, it shows a selection box, where I can choose items, but there is no way to denote quantity for each item.
I'd like to have it display a dropdown menu where you can select an item, and an input box where you can enter the quantity.
Do I have to do something custom to get it to work this way?
EDIT: I could probably just write the form in HTML myself, but I want to use the validation features of Django forms in the backend too.
It sounds like what you want is an inline formset on the through table. That would give you the selection box for the item and the input box for the quantity.

Fine tuning Django queryset retrieval

In a Django app of mine, I need to display, for a list of users (called user_ids), their:
username, avatar and score.
The username is retrieved from the User model, whereas avatar and score are present in the UserProfile model (that has a one-to-one field point to the User model, called user).
Currently my approach is to fetch the full objects (see below), even though I just need 3 attributes from the two models.
What's the most efficient way for me to just retrieve just the required fields, nothing else? Now I know i can do:
usernames = User.objects.filter(id__in=user_ids).values_list('username',flat=True)
scores_and_avatars = UserProfile.objects.filter(user_id__in=user_ids).values_list('score','avatar')
However, these give me separate querysets, so I can't iterate over them as a unified object_list and show each user's username, score and avatar in a Django template. So what would be the most efficient, light-weight way to retrieve and put this information together?
Currently, I'm doing the following to retrieve these three fields: queryset = User.objects.select_related('userprofile').filter(id__in=user_ids)
The most efficient way it's use values, your query will look like this:
usernames = User.objects.filter(id__in=user_ids).values('username', 'userprofile__score', 'userprofile__avatar')
This solution will return dictionary, but you can try to use only instead of values, it'll create django model object with specified fields, but if you will try to access not specified field it'll generate additional query.
usernames = User.objects.filter(id__in=user_ids).select_related('userprofile').only('username', 'userprofile__score', 'userprofile__avatar')