Django3: change style of selection field in admin view? - django

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.

Related

horizontal multiselect checkboxes in django admin

Hopefully this is a simple fix, although I haven't found anything through searching.
I am using this code in admin.py to make my manytomany field appear as checkboxes in admin.
class MyModelAdmin(admin.ModelAdmin):
formfield_overrides = {
models.ManyToManyField: {'widget': CheckboxSelectMultiple},
}
But, I have about 10 choices, which is annoying as a vertical list. Is there a way to get the checkboxes to display horizontally or even more flexibly as 2 columns of five choices (or some other arbitrary look)?
I discovered through another search that this can be done by modifying the widget multiple_input.html file. not the best solution, but it worked

How to filter choices in fields(forms) in Django admin?

I have model Tech, with name(Charfield) and firm(ForeignKey to model Firm), because one Tech(for example, smartphone) can have many firms(for example Samsung, apple, etc.)
How can I create filter in admin panel for when I creating model, If I choose 'smartphone' in tech field, it show me in firm field only smartphone firms? Coz if I have more than one value in firm field (for example Apple, Samsung, IBM), it show me all of it. But IBM must show only if in tech field I choose 'computer'. How release it?
class MyModelName(admin.ModelAdmin):
list_filter = (field1,field3,....)
refer:-
https://docs.djangoproject.com/en/2.1/ref/contrib/admin/
You can define the choices of the input with the attribute 'choices' of the widget. When you create the admin form of the model, you could define manually the fields, and also you can define the widget for each input. In the widget you could define with a tuple the choices and the initial values.

Change django admin's foreign key widget

I have a few models in my django project: CoursePacks, Courses and Chapters.
Courses have a many-to-many relationship to the CoursePacks, which in the admin, after some inline editing, displays this widget:
(This is a COURSE-COURSEPACK relationship)
Which allows me to select, edit and create another course which will be automatically added to the course pack.
The chapter ("capĂ­tulo") and the course models, however, are connected through a foreign key relationship, and the widget displayed on the admin is the following:
(This is A CHAPTER-COURSE relationship)
Which I have edited so that less fields are shown, because if I didn't all the fields and the entire textarea content would be displayed.
When I click on the add or edit button to the side of the course instance on the course pack admin window, a window pops up which allows me to edit or create another course.
I would like to be able to have a similar mechanism but for creating chapters through the course admin window. Will I have to edit the admin's markup or is there a widget editing functionality that does what I need?
If not, where could I begin to do so?
Ok, I ended up finding the answer for this question a little while later. I forgot to post it here, but now I was reminded because I just got a "tumbleweed badge" for this question.
class ChapterInline(admin.StackedInline):
model = Chapter
view_on_site = False
exclude = (
'slug', 'text',
)
show_change_link = True
The solution for this issue was merely this show_change_like = True piece of code. As for the rest of the code above, it is useful as in it makes the appearance of the inline (below) cleaner.
The view_on_site configuration is set to False simply because the "View on site" link was not working and I didn't think fixing it was worth the stress, for design reasons.
#admin.register(Course)
class CourseAdmin(admin.ModelAdmin):
inlines = [
ChapterInline,
]
#...
Which has also had its non-related code taken out, for relevance reasons.
This is the result:
Which is not really perfect, but it does the job. The only issue is that first a chapter has to be created before it can be edited.

Django admin, override dropdown with selection from popup window

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.

django admin inlines (and nested inlines) : how can I get this functionality?

I'm a little confused as to why this sort of functionality isn't default in the admin, but maybe someone can give me a few hinters to how to go about it.
I have a projects application which keeps track of projects and is to be edited through the admin. Each project has numerous ForeignKey related models (links, flatpages, video, image etc.) that could be placed as inlines within the project admin.
(One or two models have nested inlines, so they don't display in the admin (this and this ticket deal with this) )
Instead of being able to edit these models inline on the project admin (which gets messy and difficult to use), I would love a list of all the current instances of that related model, and simple add/edit button for each model which opens a popup with that model's form.
Project Admin:
- Normal Fields
- Links:
-Link 1 (edit)
-Link 2 (edit)
+ add link <- popup
- Images:
-Image 1 (edit)
-Image 2 (edit)
+ add image <- popup
so on. How would I go about writing this? I only need to do it for one section/model of the admin panel so I don't think writing my own Crud backend is necessary.
Thanks
I implemented something like this in an application once, but since django-admin doesnt support nested inlines (by which i mean inlines within inlines), i followed a slightly different approach. The use case was that you had an invoice (with a few inline attributes) and u had reciepts (again with inline attributes). Reciepts had a foreign key to the invoice model (basically a reciept was part payment of the invoice).
I implemented it by adding a field to the invoice list view which linked to a filtered reciept list view.
So in the invoice admin, there would be:
def admin_view_receipts(self, object):
url = urlresolvers.reverse('admin:invoice_%s_changelist'%'receipt')
params = urllib.urlencode({'invoice__id__exact': object.id})
return 'Receipts' % (url, params)
admin_view_receipts.allow_tags = True
admin_view_receipts.short_description = 'Receipts'
This gives you a link in the list view that takes you to another list view, but filtered by foreignkey. Now you can have inlines for both models and easy access to the related models.