Login

Tag "change_list"

Snippet List

Keeping filter states after edits (Django 1.4)

When saving an edit to an object from a filtered list view you are, by default, returned to list view without any of your filters applied. This solves that problem, keeping the filtered view in a session variable until you reach a point where the session key is deleted. This solution doesn't require changes to template code...this is completely self contained within your own admin.py files. The solution presented here is a revision of a previous patch, which has been modified for Django 1.4. The Django 1.3 version exists at: http://djangosnippets.org/snippets/2531/ From chronosllc: The advantage to our solution over the above linked solution is that under different use cases the user may or may not be redirected to the filtered list_view. For example, if you edit an object and click the save and continue button, then you would lose the filter when you finally finished editing the object and clicked save. Added on here is a delete of the session key when users add objects, the reasoning we're going this route is we don't want to return users to filtered views when they just added a new object. Your mileage may vary and if so, it's easy enough to fit your own needs.

  • filter
  • admin
  • change_list
Read More

Filter change list by a date range

The Django admin site has a feature to filter objects in change list by parameters supplied in the query string. Particularly, parameters such as date\__gte and date\__lte can be used. This example is for filtering objects in change list by a date range (the date field is called expiration_date, but you can, of course, use any other name). As the server side (Django) already supports such filtering, all you need to do is to edit this for your needs (you can also add some DRY if you want) and put it into templates/admin/app_name/model_name/change_list.html. Some CSS for better layout: div.date_filter select#from_month, div.date_filter select#to_month { margin-left: 0; } div.date_filter label { margin-right: 5px; }

  • javascript
  • date
  • jquery
  • html
  • change_list
  • change-list
  • date-range
Read More

keeping filter states after edits

When saving an edit to an object from a filtered list view you are, by default, returned to list view without any of your filters applied. This solves that problem, keeping the filtered view in a session variable until you reach a point where the session key is deleted. The solution presented here is hugely based off of other's work with most of the solution gained from: [Admin: return to change_list with filter and pagination applied](http://djangosnippets.org/snippets/2415/ "Admin: return to change_list with filter and pagination applied") This solution offered the best approach in our mind over the others listed here on snippets since the solution didn't require changes to template code...this is completely self contained within your own admin.py files. The advantage to our solution over the above linked solution is that under different use cases the user may or may not be redirected to the filtered list_view. For example, if you edit an object and click the save and continue button, then you would lose the filter when you finally finished editing the object and clicked save. Added on here is a delete of the session key when users add objects, the reasoning we're going this route is we don't want to return users to filtered views when they just added a new object. Your mileage may vary and if so, it's easy enough to fit your own needs. HTHs

  • filter
  • admin
  • change_list
Read More

Return to change_list with filter after change

This snippet allows you to return back to the filtered change_list after clicking "Save" on a change form. Other snippets I've found don't seem to take into account clicking on "Save and add another" or "Save and continue"

  • filter
  • admin
  • change_list
Read More

Admin: return to change_list with filter and pagination applied

By default every time you change and save an object in the admin, the change_list "jumps" to the first page, so filters you used to find the object (or the pagination-page) have to be applied again. If you have to go through a multi-object-list step-by-step this could become really annoying. The above snippet changes this behaviour by returning to the referring URL when saving. Included in this URL are variables for the filters/pagination. The snippet is part of your custom Model.admin in admin.py.

  • filter
  • admin
  • pagination
  • change_list
Read More
Author: fx
  • 4
  • 8

Additional Change List Columns

Ever wanted to add a link or two at the end of a row in a model's change list? Say you had a model for people and a model for registrations or blog posts and you want to modify the people change list so it has a link to, say all of the registrations or blog posts for the person. Well, Django provides half of the solution already in that the example registration change_list already handles the display of all registrations tied to that person. For example, the url `/admin/registrations/registration/?person__id__exact=121` gets you to a filtered list of registrations for the person with the id of 121. This is the same url used if you use list_filter in your model definition (though setting list_filter is not required for what we're doing). Okay, so to add a link to the end of each person row in the change list, you need to create a template tag similar to "person_result_list" in the code. There, I've given an example that adds two links. Each dictionary in additional_links needs to have at least a text, sortable, and url_template attribute. The text attribute is what will display as the header to the column. url_template will be fed the id of the row object (in this example, a person id), which you can use to construct the link for each row. You could extend this if you wish, though all I ever need is the id. And the last step is to use your new template tag in a modified change_list.html in place of the default result_list tag. The example at the bottom of the code shows an example usage of the tag. Hope this makes sense and is helpful to someone!

  • template
  • admin
  • tags
  • change_list
Read More

6 snippets posted so far.