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
Related
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.
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.
I'd like to create a confirmation page for selected objects before a change is made to them (outside the admin). The objects can be of different models (but only one model a time).
This is much like what is done in administration before deletion. But the admin code is complex and I haven't grasped how it is done there.
First I have severall forms that filter the objects differently and then I pass the queryset to the action / confirmation page. I have created a form factory so that I can define different querysets depending on model (as seen in another similiar question here at Stackoverflow):
def action_factory(queryset):
''' Form factory that returns a form that allows user to change status on commissions (sale, lead or click)
'''
class _ActionForm(forms.Form):
items = forms.ModelMultipleChoiceField(queryset = queryset, widget=forms.HiddenInput())
actions = forms.ChoiceField(choices=(('A', 'Approve'), ('D' ,'Deny'), ('W' ,'Under review'), ('C' ,'Closed')))
return _ActionForm
Which I use in my view:
context['form']=action_factory(queryset)()
The problem is that the items field wont be displayed at all in the html-code when it is hidden. When I remove the HiddenInput widget it displays the form correctly.
I don't want to display the choice field since there can be thousands of objects. All I want to have is something like "Do you want to change the status of 1000 objects" and a popdown and a submit button. A simple enough problem it seems, but I can't get it to work.
If someone has a solution to my current attempt I would be glad to hear how they have done it. Even better would be if there is a cleaner and better solution.
I used the wrong widget. It should be MultipleHiddenInput not HiddenInput.
i have an app that have 2 fields: company_name and logo, i'm displaying the companies like radiobutton in my Form from Model, but i want to show the logo company instead of the company label (company name)
Any idea ?
My forms:
class RecargaForm(ModelForm):
compania = forms.ModelChoiceField(queryset=Compania.objects.all(), initial=0 ,empty_label='None', widget=forms.RadioSelect())
class Meta:
model = Recarga
Thanks :)
You could try something similar to the technique I propose in the answer to this (my own) question: How do I create a Django form that displays a checkbox label to the right of the checkbox?
Instead of writing a filter that flips the markup around, you'll need to somehow get the filter to replace the labels with the appropriate images.
Admittedly, this sounds kinda awkward to work with.
The default django 1.0.2 ManyToManyField widget (a multi-select) is difficult to use when there are a lot of things in the select box. Is there another widget available that gives a comma separated list of id's in a textarea? If this is not available what do I need to do to write one, and have it show up on ModelForm.as_p() and in the admin site?
If there are no existing widgets that do what you want (and I don't think there are) then you'll need to write your own. Unfortunately, the Django documentation doesn't show you how to do this, but it's not hard to figure out by looking at the source-code forms/widgets.py copying an existing widget and modifying it.
I believe setting raw_id_fields on the manytomanyfield actually outputs a TextInput widget with a comma-separated list of ids.
You may just override this in admin.py, in the corresponding ModelForm and force a TextArea widget on it.
In the Admin you can use filter horizontal and/or filter vertical:
class MyModelAdmin(admin.ModelAdmin):
filter_horizontal = ['many_to_many_field_name']
filter_horizontal = ['another_many_to_many_field_name']
doc