Django admin, override dropdown with selection from popup window - django

In a one-to-many relationship, how can I override the drop-down menu in the change-form to be able to select value from popup window, specially when dropdown could hold a pretty long list which may slowdown the page load.

Just found the answer:
raw_id_fields is a list of fields you would like to change into an
Input widget for either a ForeignKey or ManyToManyField:
class ArticleAdmin(admin.ModelAdmin):
raw_id_fields = ("newspaper",)
https://docs.djangoproject.com/en/1.6/ref/contrib/admin/#django.contrib.admin.ModelAdmin.raw_id_fields
The question now is how can I display a user friendly value instead of the Id returned from the popup.

Related

Django3: change style of selection field in admin view?

In my django project I have a project model with a many to many field for users, which can be added and removed. This works well, however, the selection field in the admin view is a bit inconvenient. Selection works by clicking on the usernames. To select multiple users CTRL has to be held. If you do that wrong all selections can be removed just by clicking on a certain item.
So, it is easy to loose the current selection with this tool. A better tool would be something that works like the user-groups and -rights window, with two separate panels, unselected and selected items.
My question is whether this tool:
can be replaced this tool:
So that users not joined the project are listed on the left and joined users are on the right?
Or vice versa.
And what is the name of the second widget?
In admin.py add filter_horizontal to the admin-class, for example:
class ProjectAdmin(admin.ModelAdmin):
readonly_fields = ("pk", "path", "slug", "created", "path_exists", "created_by")
list_display = ("pk", "name", "path", "path_exists", "created_by", "created")
filter_horizontal = ('users',)
This will display the second widget.

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.

Django disable widget caching?

So I have a form where I defined a select widget like this:
class AdHocVoucherTemplateForm(ModelForm):
class Meta:
model = AdHocVoucherTemplate
widgets = {
'retailer_id': Select(choices=[(r.pk, r.name) for r in Retailer.objects.all()]),
}
This way I achieve a select input field with all retailers. User can select a retailer from a drop down list and submit the form.
The problem I noticed is that when I add a new retailer (Retailer.objects.create etc), it doesn't appear in the form in the drop down list. It appears to be cached. When I restart the uwsgi service running Django, it is there.
How can I make the widget always refresh the newest values from the database?
I don't see this caching behavior when I do something similar with a ModelChoiceField.
It's default widget is a Select.
Something like:
retailer = forms.ModelChoiceField(queryset=Retailer.objects.all())
When your code is evaluated, that choices parameter is constructed once and then your select just has a static list of retailer (id,name) tuples. When the ModelChoiceField is constructed, it is referencing a QuerySet which is not evaluated until the list of choices is actually requested/displayed.

Filtering a django choicefield in admin form on the basis of a value of another choicefield in the same form

In the admin form, how do you filter a django choicefield on the basis of value of another choicefield.
For example, if a choicefield is having "fruits" and "vegetables", then on selection of fruits, the second choicefield should have "apple,pear,orange" etc.Similarly one selection of vegetables the field should show "brinjal,lady finger,cabbage"
Thanks in advance
You could probably accomplish that with grouped selects in django-smart-selects. The default is outputting a drop down menu. And you should be able to modify it to output radio buttons instead.

Django Custom Widget For ManyToMany field

Does anyone know of a widget that displays 2 select boxes. One shows a list of all object in a model and the other shows the objects which have been selected. The user can then select an object from the first list, click an >> button which moves it to the 'selected' list. Then when the form is saved the objects in the selected list are saved in the manytomany field.
Thanks
django.contrib.admin.widgets.FilteredSelectMultiple