I have registered some models to display in the admin area, but I would like for some fields to be hidden.
As an example, I have a TeachingClasses model with a BooleanField named 'Status' that is set to True or False depending if the class is open or not. But that is set somewhere else in the app. There is no need to have that field displayed in the admin area when someone wants to create a new class to attend.
As such, is there a way to hide that field in the admin area?
I have tried adding this to the app admin.py file but it did nothing
from django.contrib import admin
class MyModelAdmin(admin.ModelAdmin):
class TeachingClasses:
exclude = ('Status',)
but it's not working?
Any clue if this is the right way?
My model:
class TeachingClasses(models.Model):
name = models.Charfield('Class Name',max_lenght=64)
[...]
status = models.BooleanField('Status',default=True)
What you did is not the correct syntax, you need:
class TeachingClassesAdmin(admin.ModelAdmin):
exclude = ('status',)
admin.site.register(TeachingClasses, TeachingClassesAdmin)
Django doc about how to use exclude.
In the admin.py:
class TeachingClassesAdmin(admin.ModelAdmin):
list_display = ('name',) # plus any other fields you'd like to display
admin.site.register(TeachingClasses, TeachingClassesAdmin)`
Related
On a Django admin edit page, I would like to add a little search box to be able to filter a list of tags. The field tag is a many to many field.
Is there a special trick?
you can't add search field inside the box but you can search for the same field value at the top
add this to your admin.py file
#admin.py
from django.contrib import admin
from Your_app.models import Your_model
class example_class(admin.ModelAdmin):
search_fields = ['ManyToManyFiledname']
admin.site.register(Your_model,example_class)
I found the documentation on https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.autocomplete_fields
The code below adds a search box:
class TagAdmin(admin.ModelAdmin):
search_fields = ['name']
class CompanyAdmin(admin.ModelAdmin):
autocomplete_fields = ['tags']
Some of my models have postgres-specific django.contrib.postgres.fields.DateTimeRangeFields, and those fields are exposed in the corresponding admin panels. I expected that the ranges forms would consist of two Django-style datetime pickers, with a separate one for the date part and a separate part for the time part (just like the DateTimeField would). However, I get two text inputs which expect input in a very particular format. Is there anything I am missing or have to configure separately?
The relevant code is:
from django.contrib.postgres.fields import DateTimeRangeField
...
class MyModel(models.Model):
time_off = DateTimeRangeField()
admin:
#register(MyModel)
class MyModelAdmin(admin.ModelAdmin):
pass
You are looking for SplitDateTimeWidget.
Simply change the admin part as:
class MyModelAdminForm(forms.ModelForm):
class Meta:
model = MyModel
widgets = {
'time_off': RangeWidget(SplitDateTimeWidget())
}
#register(MyModel)
class MyModelAdmin(admin.ModelAdmin):
form = MyModelAdminForm
or use formfield_overrides to override the widget if you wish.
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)
My model looks like this:
class Search(models.Model):
user = models.ForeignKey(User)
regions = models.ManyToManyField(Region)
class Region(models.Model):
name = models.CharField(max_length=100")
In my admin when I register Search model and Region model,
I want to see the User data in the Search model and search and user data in Region model.
both in the list_display and inlines of the admin. Since one is a ForeignKey and one is ManytoManyField, I am not clear how to get this working.
some help will be much appreciated
Thanks
You aren't going to be able to get the User data visible on the inline on the Region admin screen easily. Below is a decent starting spot.
from django.contrib import admin
from django.contrib.auth.models import User
from .models import Search, Region
class UserInline(admin.TabularInline):
model = User
class SearchAdmin(admin.ModelAdmin):
inlines = [
UserInline,
]
class SearchInline(admin.TabularInline):
model = Search
class SearchRegionsInline(admin.TabularInline):
model = Search.regions.through
class RegionAdmin(admin.ModelAdmin):
inlines = [
SearchRegionsInline,
SearchInline,
]
If you're set on editing the user information from the Region admin screen, then you will want to create a custom form for the SearchInline so it has the fields from the User model and then populate the values in the __init__ if an instance is passed in.
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