78š
You can order the fields as you wish using the ModelAdmin.fields
option.
class GenreAdmin(admin.ModelAdmin):
fields = ('title', 'parent')
9š
Building off rbennellās answer I used a slightly different approach using the new get_fields method introduced in Django 1.7. Here Iāve overridden it (the code would be in the definition of the parentās ModelAdmin) and removed and re-appended the āparentā field to the end of the list, so it will appear on the bottom of the screen. Using .insert(0,āparentā) would put it at the front of the list (which should be obvious if youāre familiar with python lists).
def get_fields (self, request, obj=None, **kwargs):
fields = super().get_fields(request, obj, **kwargs)
fields.remove('parent')
fields.append('parent') #can also use insert
return fields
This assumes that your fields are a list, to be honest Iām not sure if thatās an okay assumption, but itās worked fine for me so far.
- [Django]-Django ā view sql query without publishing migrations
- [Django]-What is the advantage of Class-Based views?
- [Django]-Django set range for integer model field as constraint
6š
I know itās an old question, but wanted to thow in my two cents, since my use case was exactly like this, but i had lots of models inheriting from one class, so didnāt want to write out fields for every admin. Instead I extended the get_form model and rearranged the fields to ensure parent always comes at the end of the fields in the admin panel for add/change view.
class BaseAdmin(admin.ModelAdmin):
def get_form(self, request, obj=None, **kwargs):
form = super(BaseAdmin, self).get_form(request, obj, **kwargs)
parent = form.base_fields.pop('parent')
form.base_fields['parent '] = parent
return form
base_fields is an OrderedDict, so this appends the āparentā key to the end.
Then, extend this admin for any classes where you want parent to appear at the end:
class GenreAdmin(BaseAdmin):
pass
- [Django]-Django ā what is the difference between render(), render_to_response() and direct_to_template()?
- [Django]-How to write setup.py to include a Git repository as a dependency
- [Django]-Django admin ā make all fields readonly
1š
This simple solution from @acquayefrank (in the comments) worked for me:
The order of fields would depend on the order in which you declare them in your models.
- [Django]-How to redirect with post data (Django)
- [Django]-Getting a count of objects in a queryset in Django
- [Django]-Execute code when Django starts ONCE only?