Render a select box as radios buttons in Django Admin - django

I want to show select box options as raido buttons in Django Admin change model view. I don't want to write custom model forms. I'm looking for a way to render some select boxes as radio buttons while keeping auto generated model forms of the Django admin. I'm using django v 1.11.

Assuming my_field is the field, we want to be rendered as Radio Button
# admin.py
from django.contrib import admin
from django import forms
from .models import MyModel
class MyModelAdminForm(forms.ModelForm):
class Meta:
model = MyModel
exclude = ()
widgets = {'my_field': forms.RadioSelect}
class MyModelAdmin(admin.ModelAdmin):
form = MyModelAdminForm
admin.site.register(MyModel, MyModelAdmin)

Related

Can Django admin show the image after the model name?

I have a problem.
Can I add image after the Model name in the django admin?
The postion I want to add the picture.
I want to see the books image when I login admin page
I have no idea about it
maybe I can use verbose_name with inject html tag?
Can django do it? Or I should use others way to do it?
Basically, You could customize ModelAdmin.list_display in Django admin list view, for example:
Suppose you have a model MyModel with an image field named image
from django.utils.html import format_html
#admin.register(MyModel)
class MyModelAdmin(admin.ModelAdmin):
list_display = ('id', 'image_tag')
def image_tag(self, obj):
return format_html("<img src="{}" width=200 height=80/>'.format(obj.image.url))
image_tag.short_description = 'Image'

How can I change the django-leaflet openlayers canvas?

I have a django-leaflet widget, but the widget shows some imagery instead of openlayers map, how can i change this on modelform?
from django import forms
from django.contrib.gis.forms import OSMWidget, PointField, ModelForm
from leaflet.forms.widgets import LeafletWidget
class YourMapWidget(LeafletWidget):
geometry_field_class = 'geom'
class ApartmentForm(forms.ModelForm):
class Meta:
model = Apartment
fields = '__all__'
widgets = {'geom': YourMapWidget()}
One way is to change it globally using the settings. If you want to use OSM, just add something like this to your settings,
LEAFLET_CONFIG = {
'DEFAULT_CENTER': (6.0, 45.0),
'DEFAULT_ZOOM': 16,
'TILES': 'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png'
}
BTW, it's actually weird, the default settings should be OSM map.
django-leaflet docs

Django: Search many to many field while creating object

I've got a use case where I have multiple Ingredient that can be linked to a Recipe through the Django admin. Now I have around a hundred ingredients which makes it very difficult to select the ingredients in the following UI.
Is there a way to add a search field or something similar to the django admin for easier selection?
You have few choices.
1. filter_horizontal
With filter_horizontal, you can use horizontal m2m ui in admin. I prefer this way using m2m in admin.
class YourAdmin(admin.ModelAdmin):
filter_horizontal = ('m2m_field',)
...
And the result will be...
2. raw_id_fields docs
You can use raw_id_fields for using pop-up modal with your m2m fields.
It's bit useful when you have lots of m2m field. Also, it's easy to filter which m2m obj to add.
class YourAdmin(admin.ModelAdmin):
raw_id_fiedls = ('m2m_field',)
...
I suppose you want to filter over ingredients and select it one by one on admin UI
You can use django forms builtin CheckboxSelectMultiple
widget in place of SelectMultiple to make selection easy
from django import forms
from django.contrib import admin
class RecipeForm(forms.ModelForm):
class Meta(object):
model = Recipe
widgets = {
'Ingredient': forms.CheckboxSelectMultiple,
}
class RecipeAdmin(admin.ModelAdmin):
form = RecipeForm
admin.site.register(Recipe, RecipeAdmin)
Alternatively, you can use django-better-filter-widget
package if you want a search input on choices, Refer Github repo for
installation
It is a custom widget, created by overriding SelectMultiple widget of
django forms
from django import forms
from django.contrib import admin
from better_filter_widget import BetterFilterWidget
class RecipeForm(forms.ModelForm):
class Meta(object):
model = Recipe
widgets = {
'Ingredient': BetterFilterWidget(),
}
class RecipeAdmin(admin.ModelAdmin):
form = RecipeForm
admin.site.register(Recipe, RecipeAdmin)

Django - using Many-to-Many horizontal interface outside of admin

I'm working in a form with a m2m field. I want that this field looks like the horizontal interface of the django admin site... ¿how i can do it?
thanks...
You need to use the FilteredSelectMultiple widget
from django.contrib.admin.widgets import FilteredSelectMultiple
from django import forms
from .models import Person
class PersonForm(forms.ModelForm):
some_field = forms.ModelMultipleChoiceField(Person.objects.all(), widget=FilteredSelectMultiple("Person", False, attrs={'rows':'2'}))
class Meta:
model = Person
You will also need to include the Javascript and CSS used in the admin. Here's an example

Adjusting how a ModelForm is displayed

I've made some changes to an admin form so that I could display a TextField like a CharField but the form itself looks pretty ugly in the admin menu as the form elements aren't stretching properly. I also don't want to display the name of model when I print it since it's already on the page. How would I make those changes? Ideally I would like the link field to take up all the remaining space shown the screenshot below.
admin.py
from linkrotator.models import Link, LinkList
from django.contrib import admin
from django import forms
class LinkModelForm( forms.ModelForm ):
link = forms.CharField( label = "Link")
class Meta:
model = Link
class LinkInline(admin.TabularInline):
form = LinkModelForm
model = Link
class LinkListAdmin(admin.ModelAdmin):
inlines = ( LinkInline, )
admin.site.register(LinkList, LinkListAdmin)
How it looks.
You need to edit the CSS for the admin section, easymode:
http://docs.djangoproject.com/en/dev/ref/contrib/admin/#modeladmin-media-definitions