In the Django admin index page, the app and its models will normally be listed. How can the model objects also be listed in this index page? Instead of displaying just the app, I want to also display its model objects. How should it be customized?
相关问题
- Django __str__ returned non-string (type NoneType)
- Django & Amazon SES SMTP. Cannot send email
- Django check user group permissions
- Django restrict pages to certain users
- UnicodeEncodeError with attach_file on EmailMessag
相关文章
- TypeError: 'BaseQuery' object is not calla
- Profiling Django with PyCharm
- Why doesn't Django enforce my unique_together
- MultiValueDictKeyError in Django admin
- Django/Heroku: FATAL: too many connections for rol
- C++ Template specialization to provide extra membe
- Django is sooo slow? errno 32 broken pipe? dcramer
- C++: How to use unnamed template parameters in cla
I wanted the same functionality for my site and added it by doing slight modifications to the core django system.
Step 1: First we need a way to indicate which models should have their properties listed. Add the following code to the models for which you want the instances listed (in models.py):
Step 2: We need to modify Django to recognize and read this new attribute. In core-django file: db/models/options.py, roughly at line 22 append 'list_instances' to DEFAULT_NAMES:
and in the same file, roughly at line 52, create a default field for this attribute right after the other attributes :
Step 3: We need to pass this information along to the template that generates the index page. In core-django file: contrib/admin/sites.py, inside index() method and inside the "if has_module_perms:" part, add the following code:
This will create the list of instances to show, but only if the list_instance attribute is set. In the same file, a few lines further down, append these values to the "model_dict" construct.
Step 4: The final step is to modify the template to support this. Either edit the core-django file /contrib/admin/templates/admin/index.html or copy this file to the templates/admin/ directory of your specific app. Add a few lines after the standard code for generating rows to generate the "sub-rows" if applicable. Roughly at line 40, right between "/tr>" and "{% endfor %}":
This will cause the item to be listed with the name generated by the unicode() method in the model.
Step 5: Lo and behold! It should look something like this:
Edit: Optional Step 6: If you want the instance names to be clickable too, just change the template (index.html) and replace:
with:
You can do this by changing the various admin templates - the root one is called
app_index.html
and controls what gets displayed there. The best way to investigate what's happening where is to install django-debug-toolbar and then look at the templates being used for each view to figure out how to customise.UPDATE Setomidor answer for django 10
Always great to come back to this clean solution!
step 2 - it is around line 125 (was 52)
step 3 - in sites.py - update the new method -
inside the for loop :
for model, model_admin in models.items():
add step 3 as said around lines 430 and 460