Dynamically sort in Django admin - python

In Django admin,
Is it possible that dynamically sort by field name using url param?
In this way,
https://www.example.com/admin/blog/article/?order=modified_date
https://www.example.com/admin/blog/article/?order=deleted_date

Depending on your version of Django, you can use the get_ordering() method in your ModelAdmin class.

Related

searchable dropdown list for adding a new post in django [duplicate]

I got a foreign key model with about 100,000 date.
It's too difficult to select the wanted data. So I'm wondering if there is a humanized way to add foreign key in django admin.
I have tried the raw_id_fields, it's useless.
this don't work.
If you have a raw_id_fields set on your source admin class, you can set the search_fields on the target admin model to be able to filter the result based on the set search_fields, i.e. you will get a search input field in the select list popup, see image below.
Alternatively, django-2.0 just merged a pull request providing support for using select2 widgets in the django admin, see pull request for the full list of supported fields: https://github.com/django/django/pull/6385
With this you can use the autocomplete_fields on a foreignkey field on your source admin class and as before set the search_fields on the target admin class.
Select2 is very handy to deal with such situation. You may check packages for Select2 integration with Django.
django-select2 and django-easy-select2 both are good option to consider.

How can I make the multi-select function in django admin

I am using the ManyToManyField in Django models and would like to change the interphase to something better rather than the default interphase.
This is the default interphase, how can I make it like a multiple select items.
You could use below option under ModelAdmin inherited class which transforms widget as in image below
filter_horizontal = ('students', 'teacher',)
also refer here

Django override custom admin site's templates

I have two admin sites in my django app. One named admin and the other named owneradmin.
I want to override their templates separately. Is there a way to do it ?
If you want to override the index template, the easiest way is to set index_template for the AdminSite.
CustomAdminSite.index_template = 'path/to/custom/template.html'
OwnerAdminSite.index_template = 'path/to/owner/template.html'
See django docs for more details about index template and template overriding.

Override template tag in wagtail cms?

I am using ModelAdmin to register a custom model with the Wagtail admin. I would like to customise the IndexView. The only thing I want to do is add a button to each row. I can see that the rows are added by the template tag - results_list, which leads me to the results_list.html in modeladmin/includes.
As this is registered with the #register.inclusion_tag decorator, is it possible to override the template in my own app? The normal django methods of overriding templates dont seem to be working for me...
You can customise the buttons for the index page by setting a custom button_helper_class on your ModelAdmin class: http://docs.wagtail.io/en/v1.8.1/reference/contrib/modeladmin/primer.html#modeladmin-button-helper-class
(Incidentally, the tag and template are named result_list, not results_list - perhaps that's why you weren't able to override it?)

how do you style a form already inside django

I'm using django-registration. I'd like to update the AuthenticationForm in 'django.contrib.auth.forms'. Specifically, I would like to pass the 'attrs' dict to add a few attributes. How do I update a form that comes with django?
You can use the views that come with auth and override the form parameter with your own form:
django.contrib.auth.views.login(request[, template_name, redirect_field_name, authentication_form])
More info here.
The standard way is to subclass AuthenticationForm, change the attrs in constructor, pass the form to login view and write a new entry in urls.py.
This is a nightmare: in order to add html attribute to a field it is necessary to use python subclassing, to know how exactly django's form metaclass work (self.fields['field'].widget.attrs, not just self.field.widget.attrs), to know regexes (for urls.py), to know how django's urls.py work (should you put the overriding line before of after include('django.contrib.auth.urls')?) and to know where is the auth form and auth view imported from.
And now the "commercial break": just use http://pypi.python.org/pypi/django-widget-tweaks for your task ;)

Categories