Django admin filter on edit page - django

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']

Related

Customizing Django Admin Search

I want to customize my Django Admin Search form.
Like this picture, I want to add select box to choose search fields.
Is there any built in parameter in Django? or Is there other way to make this possible?
enter image description here
I think you have to add search_fields to your model admin class, like below:
# admin.py
class MyModelAdmin(ModelAdmin):
...
search_fields = (
'field1',
'field2',
'field3',
)
This way the search will only apply to these fields of your model.

Django 1.11 admin form add search box to oneToOne field

I have a user, admin and employee models, both employee and admin are related to the user model with a oneToOne field what i would like to do is in the admin form i have one field "user"
instead of having a drop down field with all users i would like to have a search box so that when i search for a specific user i find him and choose him to be an admin.
how?
i also tried to add user's fields to admin's form but i couldn't
i tried the inline thing, the parent_link in the admin's model... but i couldn't find a solution
Now i would like to minimise the search through the long users list by adding a search box to the field
I have solved this type of problem using django-ajax-selects package. Hope this will help you.
app/admin.py:
from ajax_select import register, LookupChannel
#register('users')
class UsersLookup(LookupChannel):
model = User
def get_query(self, q, request):
return self.model.objects.filter(username__icontains=q)
class EmployeeAdminForm(forms.ModelForm):
user = AutoCompleteSelectField('users', required=False,
help_text=None)
class Meta:
model = Employee
fields = '__all__'
settings.py
AJAX_LOOKUP_CHANNELS = {
'users' : {'model': 'auth.user', 'search_field': 'username'},
}
Use django's raw_id_fields. With that you can declare a foreign key object searchable.

Hiding a model field in Django admin 1.9

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)`

Adding headers to columns of admin app page in Django

I'm customizing the admin panel for my django app and saw that someone pre-1.8 was using list_display = ['join', 'email', 'timestamp', 'updated'] to add a header to the columns of his django app page and separated sections based on his model.
I've tried list_display in django 1.8 and I am failing to see any header names being posted. Is there a new command for this?
Fixed:
from django.contrib import admin
from .models import Join
class JoinAdmin(admin.ModelAdmin):
list_display = ('emails', 'timestamp')
class Meta:
model = Join
admin.site.register(Join, JoinAdmin)
Issue needed to throw in second parameter in the admin.site.register of JoinAdmin

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