I have this file:
templates/admin/{app_name}/{model_name}/add/change_form.html
This extends
admin/change_form.html
I added a button to this template and it is working fine WHEN adding a new instance of the model.
I want to add another button the template that appears WHEN changing already existing instance of a model. So I created this file:
templates/admin/{app_name}/{model_name}/change/change_form.html
thinking that add and change use the same template. But changes I made to this template do not seem to take place.
What template does Django use when changing the fields of already existing instance of a model?
You can specify the change_form_template in your model admin.
By default it's (in this order):
admin/APP/MODEL/change_form.html
admin/APP/change_form.html
admin/change_form.html
The model properties could be passed in GET to the template url.
Related
I am using Django 1.11, in one of my models I have added actions when the model is saved.
However I don't want these actions to be done when only a part of the model is saved.
I know that update_fields=('some_field',) can be used to specify which field must be saved.
But, when the object has been fetched in the database using the methods only() or defer() I don't see any information about the fields updated in the save() method, update_fields is empty.
Thus my question: How can I get the fields saved by Django when only some fields have been fetched ?
When you use defer or only to load an instance, the get_deferred_fields() method returns a list of field names that have not been loaded; you should be able to use this to work out which ones will be saved.
My goal is to put a custom input field in the filter list box on my Django interface. I found no way of doing this with standard tools so I want to create a new template for the filter box.
In the official Django doc on the admin interface, there's this:
It is possible to specify a custom template for rendering a list filter:
class FilterWithCustomTemplate(admin.SimpleListFilter):
template = "custom_template.html"
See the default template provided by Django (admin/filter.html) for a concrete example.
But no more details are given. How am I supposed to implement this? filter.html does not extend change_list.html and I don't know where I am supposed to put my new custom filter template (I tried in templates/admin/my_app_name with the rest of my other overriding templates but it doesn't work)
How can I create a new template for my admin filter box ?
I am looking for information on how to add entirely new pages to the django admin interface. I need to add a view that allows admins to change the contents of an existing text file. This view needs to exist within the existing django admin app. I am using django 1.9.
I found information on extending existing pages, but not adding entirely new pages. Is this possible?
Once you've written your view, you can include it in the admin by overriding get_urls.
An alternative is to override the AdminSite class, which also has a get_urls method. However this will require changing more code if you are not already using a custom AdminSite subclass.
Assuming I have a model that directly correspond to a ModelFormset.
Assuming three instances of the model are saved in the database.
Assuming I loaded the ModelFormset with initial data = the three instances
Now I render the ModelFormset on a page for users to modify.
After modification, users click on submit. How do I know which one of the ModelFormset correspond to which instance of the Model saved in the database?
Update:
I was reading this example: https://docs.djangoproject.com/en/1.9/topics/forms/formsets/#formsets-initial-data
In this example, the initial data was provide manually. Assuming if the the initial data was passed in like this:
article = Article.objects.get(pk=...)
formset = ArticleFormSet(initial=[
model_to_dict(article)
])
When this formset is sent to the template, is article's id preserved in the rendered HTML? If not, then how does Django know which article it should update if modifications to the article has been made and submitted?
If you have an existing instance in ModelForm (or in a set of them inside ModelFormset), then there's a hidden field with the value of the primary key for the record.
Each ModelForm has also a unique suffix for each the field, which helps distinguish which fields belongs to the same model.
I need to add a custom view to a model admin similar to the history view. For example if I have a model called Job I can access the history by going to /jobs/job//history/. How do I add another view that will respond to a pattern like /jobs/job//workflow/?
You can define get_urls() on your Admin to add more admin views.
don't forget the admin_view() wrapper
if you wanted to add a view for an individual object (like the change form), just add the object id to your url pattern, then in your view (try to) grab the corresponding object.
It's up to you to provide the links (for example, by overriding the base (/change_form) template and adding a new item to the "object-tools" list).