How to make radio buttons in Django admin - django

Does Django itself provide radio button models?
I was trying to find out how to make radio buttons in Django admin, but since there is nothing related to that, It seems like Django doesn't have radio buttons as default.

This is too late but could be useful for other
you can define the radio button in your admin like this:
radio_fields = {'gender': admin.VERTICAL}

Django model with choices will make use of select by default. To use radio buttons, you will have to to use the RadioSelect widget. See django docs

Related

is there any way to add button to django admin?

i want a button to call a view function from django admin. i want my button here my django admin panel
When i click on that button , it calls a specified view.
You have to override the default django admin template. Please read more here: https://docs.djangoproject.com/en/3.0/howto/overriding-templates/

Django how to change the admin panel template

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.

Django admin - adding actions in Add page view

I have a question about admin: i've defined an admin action 'create pdf', now i can see it in the select widget of the changelist view.
I'd like to see it as a button in the add page, i've specified both actions_on_bottom and actions_on_top to True, but i see only the usual buttons (save and add another, save and continue, save).
how can i fix it?
thanks, Luke.
Actions are only registered to the dropdown menus you'll have to add a button with javascript or override the changelist template(s)

How to customize the "Clear" checkbox for a models.ImageField in django Admin page?

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.

How to modify the way a ForeignKey field is rendered in a Django admin page to avoid browser crash?

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...