I am trying to change what is displayed when I click on a model in the django admin. For example if I have the model Book and then I go into the admin panel and click book, it will give me all the fields that I created the model with.
But I want to edit that page that shows all that data. For example, if I click on the "Book" model in the admin panel and see my fields to edit, I want to put another bit of fields on the bottom from another model. Or maybe I want to put like a "Similar Books" list. Is it possible to edit this page?
Admin customization information is here. You can do stuff like list_display or fieldsets etc.
Related
I have a User model that has these two fields:
permissions = models.ManyToManyField(Permission)
groups = models.ManyToManyField(Group)
I register the model in the admin. When I view the user model that I made in the admin section I get a multiple select menu that looks like this.
I would much prefer the much better-looking menu like the one that is in the auth user model that comes built into Django admin (looks like this)
Any ideas on what I need to do to be able to access this sort of select menu?
Thanks for your help.
Django newb from PHP (finally)
Use filter_horizontal (or filter_vertical) field in your admin class.
For example:
#admin.register(User)
class UserAdmin(admin.ModelAdmin):
...
filter_horizontal = 'groups', 'permissions', 'any_other_m2m_field'
...
Also link to Django docs:
https://docs.djangoproject.com/en/3.1/ref/contrib/admin/#django.contrib.admin.ModelAdmin.filter_horizontal
Is it possible with django to add a custom html select to an admin page in order to modify the queryset of a specific model?
For example, I have a "Product" model and inside the Products admin page I get all the product.
I would like to have an html select in that page so that the admin can change the queryset not to show ALL the products but just part of it
Thanks
I have client model which has a foreign key field to a country model.
So in Django admin, when I create a client and I select the country where this client belongs to. but the problem is the select list is too long(too many countries on this planet). Sometimes it takes just too long to get the one I need.
So I wonder if there is other widget in djano admin that provides a select-input-combo widget.
When I type in the input and it will filter out the right one for me to select.
Hope you can understand what I need here.
did you see raw_id_fields ?
you would do in admin.py something like:
class ClientAdmin(admin.ModelAdmin):
raw_id_fields = ("country",)
admin.site.register(Client, ClientAdmin)
then select widget will become something like:
Since Django 2.0 there is autocomplete_fields. From the documentation:
autocomplete_fields is a list of ForeignKey and/or ManyToManyField fields you would like to change to Select2 autocomplete inputs.
and
The Select2 input looks similar to the default input but comes with a search feature that loads the options asynchronously. This is faster and more user-friendly if the related model has many instances.
Note that you need to define search_fields in the related object's ModelAdmin since it is used by the widget.
i have a Model for a user profile in my django app that has a models.ImageField and i have an ModelAdmin for it
when a user uploads an image , in the admin page , when i go in that user's Customize page , in the ImageField section , there is the url of uploaded image and a checkbox named "Clear" and a button for updating the image. how can i change the text of that checkbox ? for example i want it to have the text "Delete" instead of "Clear"
It seems like "Clear" is hardcode.
So either you create a custom widget simply like that:
class MyClearableFileInput(ClearableFileInput):
clear_checkbox_label = ugettext_lazy('Delete')
And assign it to your form field like that
MyForm(forms.Form):
myfile=ImageField(widget=MyClearableFileInput)
Or add overwrite it in your admin
class MyModelAdmin(admin.ModelAdmin):
formfield_overrides = {
models.ImageField: {'widget': MyClearableFileInput},
}
Or you use the translation mechanisms to translate Clear into Delete. Django translation is described in the docs pretty well.
I personally, just think that it is quite some overhead for your problem, unless you are using translations anyway. I would clearly recommend the custom widget - the addtional code is really minimal.
I have a Customer model which contains a ForeignKey to a Contact model.
I have over 100,000 contacts in my DB and when I load the admin page for a specific customer, the dropdown menu for the contact is getting populated with ALL of the contacts in the database. This has recently, due to its shear length, started causing my Firefox to crash while the admin page is loading.
Is there a way to either:
replace the field with an integer
field I can manually modify to the
contact ID when necessary
replace the dropdown menu with some
alternative input method which won't
crash the browser
remove this input
from the Customer admin page
altogether
Thanks!
You can do any of the either of things you want to.
Simplest solution is the exclude the field from the admin. Just say so in the admin class.
You can change the field to be text input and display it's primary key rather than the item itself, by including it in the raw_id_fields of the admin class.
You can also replace the standard dropdown widget with the Auto complete text field input. Use the implemented widget, or other equivalents. - This is probably the solution you like the best.
You can also override the formfield_for_foreignkey method on the Admin model to customize the queryset that gets displayed in the foreign-key dropdown. You may want to checkout my implementation for displaying only the current User's (or subdomain's) added entities.
Sounds like specifying the contact field in raw_id_fields in your admin.py entry for the relevant model would sort you out. Docs are here.
PS. Surprised (but not that surprised) that FF gives out before your database server tanks...