Login

Snippets by karolyi

Snippet List

Hand over model instance relations to another instance of the same model

If you have a model instance you want to merge into another, it's handy to hand over all the relations into the model you want to merge into, so the deletion won't trigger cascading deletions from other tables. You can pass an `Iterable` of the same objects (a.k.a `QuerySet`) to the model, and then process it with the new model's `pk`.

  • foreignkey
  • many2many
  • relations
  • handover
Read More

Ellipsis paginator decorator with first and last two items

Use this template tag to get a paginator showing the first and last two pages w/ adjacent pages using ellipsis. The `page` parameter is a page of a `Paginator` (typically the first but you can use whichever you want). In case of 50 pages, while being on the 40th, it'll give you the following iterable of `int`s (with `settings.PAGINATOR_ADJACENT_PAGES = 2`): `(1, 2,    38, 39, 40, 41, 42,    49, 50) ` You get the idea.

  • decorator
  • paginator
  • ellipsis
Read More

Smart wrapping Django admin's delete_selected

The idea here is to wrap the original `delete_selected` functionality in a way that I shouldn't have to reimplement the templates (confirmation/error response) serving, just extend the original. What this code does, it wraps the queryset's delete function with a closure, so when it really gets called (after the confirmation), it executes the extra functionality you wish to. After looking at the original code, this seemed to be the most efficient way of doing it.

  • admin
  • wrap
  • delete_selected
Read More

karolyi has posted 3 snippets.