I have 4 OneToOne relations to a model, and in the admin area I would like to group them so they can be edited in pairs(2 groups of 2). Is there a way to do this similar to using fields?
You can specify the field order (https://docs.djangoproject.com/en/1.5/ref/contrib/admin/#django.contrib.admin.ModelAdmin.fields) and also create fieldsets (https://docs.djangoproject.com/en/1.5/ref/contrib/admin/#django.contrib.admin.ModelAdmin.fieldsets) for the admin interface
Related
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.
For establishing many-to-many relationships, django admin has a nice widget called 'filter_horizontal'.
The problem is - it only displays primary key of the related table.
How do I add other fields from the related table?
For example, if I have a many-to-many relationship in my Order model with User model, in 'Order' django admin I can only see User's primary key(id). How do I add their name into the widget?
Turns out it is changed in str method for the User model
When in a User object in the Django Admin, there is a really nice way to add permissions to the user.
For a completely different model, I would like to use the same system. This model has a manytomany field to another model. Is there a way to copy the layout of the user admin, to this model's admin?
Add your field to the ModelAdmin's filter_horizontal:
#admin.register(MyModel)
class MyModelAdmin(ModelAdmin):
# ...
filter_horizontal = ['my_many_to_many_field']
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.
In django-admin, It's not convenient to edit a manytomany field in a new pageļ¼besides this, I wanna show more details in edit_inline area as foreignkey field does. What should I do to achieve this needs
This is foreignkey field:
and this is manytomany field:
can I edit manytomany field in this page? At least show some column in this area.
For the manytomany try using
(in admin.py)
filter_horizontal = ('fieldname', )
about the foreignkey, create a form for that inline and do something like...
forms.CharField(label='',required=False,
widget=forms.Textarea(attrs={'style': 'height: 20em;'}))