This scenario happens way too often in my project:
Foo
that has several ForeignKey
fields, one of them to refers to model Bar
Bar
has millions of instancesFoo
's admin page; Django tries to retrieve all Bar
s at once (to display them in a combo box) and the server gets overloadedFoo
's admin and adding bar
to raw_id_fields
.I'd like to prevent this situation from happening in the future, preferably by somehow stating (once and for all) that Bar
has many rows and it should be always treated as if the field referring to it was listed in raw_id_fields
in all admin pages. Is this possible somehow?
Set fieldsets to control the layout of admin “add” and “change” pages. fieldsets is a list of two-tuples, in which each two-tuple represents a <fieldset> on the admin form page. (A <fieldset> is a “section” of the form.)
You can add another class called Meta in your model to specify plural display name. For example, if the model's name is Category , the admin displays Categorys , but by adding the Meta class, we can change it to Categories . Save this answer. Show activity on this post.
Yes, you can use list_display to display Many-to-Many fields in Django Admin.
The default templates used by the Django admin are located under the /django/contrib/admin/templates/ directory of your Django installation inside your operating system's or virtual env Python environment (e.g. <virtual_env_directory>/lib/python3. 5/site-packages/django/contrib/admin/templates/ ).
This is an excelent point. This is a critical issue that can turn down the database and even the web server.
Considering this, I believe the default approach MUST be the raw_id_fields
thing. If you know what you are doing, then you change this behavior.
Unfortunately, most of the authors of the admin interfaces libraries disagree of this thought. Not just for Python-Django, but also for other communities like Ruby-Rails.
5 years ago I got tired having the same problem, then I developed the django-smart-autoregister, that do this and also auto configure using another good patterns. Even today I face this problem, so I guess it is worth to take a look.
ps: the library was originally implemented using a modular approach, though you just call some functions that will configure the raw_id_fields
for you according to the model field.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With