Login

All snippets written in Python

Snippet List

Quick line profiler decorator

Requires [line_profiler](https://pypi.python.org/pypi/line_profiler) pip install line_profiler Will print profile info into console @line_profiler def my_view(request): context = some_quick_func() return some_heavy_func(context) It does not work good when nested, so don't wrap `some_heavy_func`. If you want to profile also some nested call - use `extra_view` parameter: @line_profiler(extra_view=[some_heavy_func]) def my_view(request): context = some_quick_func() return some_heavy_func(context) It will profile `my_view` and `some_heavy_func`

  • profile
  • decorator
  • line_profiler
Read More

Template Tag to protect the E-mail address

Update to https://djangosnippets.org/snippets/1907/ to be a bit more flexible, and code cleaned up a tiny bit. To use, add this snippet as a file in a templatetags folder in an app or in a project. Then include and call the tag with {% obfuscate_email 'email' %} or {% obfuscate_email 'email' 'link_text' %} if 'link_text' is provided, it will be used as the text in the body of the <a> tag. If not, then the email will be used.

  • email
  • spam
  • obfuscate
Read More

Iterable SelectDateWidget

I wanted to be able to restyle the inputs, which required having access to each of the select widgets. When used in a form, you can simply iterate over the field to access each element. Example: {% for form_field in form.date %} <div class="select-wrap"> {{ form_field }} </div> {% endfor %}

  • widget
  • selectdatewidget
Read More

Post-post-syncdb and post-post-migrate

The `post_syncdb` (Django pre-1.7) and `post_migrate` (>=1.7 and South) signals are fired for every single app. What I really wanted was one signal fired after the migration or syncdb completed. There's no official one, and all the snippets were doing horribly hacky things (and wouldn't work for South, anyway). This one will work for Django syncdb, and South migrate.

  • post-migrate
  • post-syncdb
Read More

Grouped Model Choice Field

This class lets me have a model choice field that includes optgroups . Django has built-in support for optgroups if you explicitly set all the choices in a ChoiceField, but that doesn't help with ModelChoiceFields. Optgroups let you have nice-looking subscategories in huge dropdowns. They're even more useful if you're using something like selectize.js, because you have a ton of options. If you inherit from GroupedModelChoiceField and override the optgroup_from_instance function, as in SampleChoiceField, you'll get a dropdown with your models with the expected optgroup tags in the html. Be sure to have your queryset first order by whatever you're displaying in optgroup.

  • ModelChoiceField
  • OptGroup
  • Forms
Read More

Enhance django postgresql orm for trigram search

This is a quite simple snippet to integrate postgresql trgm search in django 1.6.10 This snippet is easy to adapt to other special operators by changing the trgm_search function. This example uses the operator `%%` but you could use `ts_vector(fieldname) @@ to_tsquery(%s)`

  • django
  • orm
  • postgresql
  • trgm
Read More

Custom Auth Backend to use E-Mail in Django

Instead of the default Django User the Auth Model is 'customer' from the usercp App. User's can use username as a display to the public, without disclosing their login information. This way they can use a forum with their username, which is seen by everyone. The login credentials, however are more privat, since it is the e-mail address. Allowing the user to not disclose any information.

  • django
  • authentication
  • email
Read More

CBV: PreviewMixin

PreviewMixin adds a preview page for Django's CBV (FormView, UpdateView, CreateView). After a form has been submitted, it is returned again, optionally in a different template to confirm. If the form is submitted with the same data, the default "form_valid" function is executed. Features: 1. `process_preview` - function executed after submitting the form for the first time (default is to do nothing) 2. `done` - function for the action if the confirm page is sent (defaults to whatever form_valid of the django cbv does) 3. `preview_template_name` - variable with the name of the template for the confirm page (defaults to taking the same template as for the initial form) 4. new function `security_hash` to calculate a hash which is added to the confirmation form. Works kind of like django-formtools, just as a Mixin for the default Django cbv.

  • django
  • mixin
  • cbv
Read More

Circular reference with Django ORM and Postgres without breaking NOT NULL FK constraints

The recipe uses deferred constraint validation to create circular references across database tables. The example requires Postgres (MySQL doesn't support deferred constraints). To achieve this, the following is required: * Insertions must be performed in a transaction. Foreign key constraints will be validated at the end of the transactions, allowing for insertion of rows with FKs pointing to rows that don't exist yet. * Primary keys need to be generated before insertion. That's what `prefetch_id` does by pulling the next value from the `*_id_seq` sequence.

  • django
  • orm
  • postgres
Read More

Allow multiple field sorting in a single admin table column

This snippets extends ModelAdmin in order to allow multi field sorting in admin tables. Usage example: class MyModelAdmin(MultiFieldSortableModelAdmin): list_display = ( ... 'user_full_name', ... ) def user_full_name(self, obj): return obj.user.get_full_name() user_full_name.admin_order_field = ['user__first_name', 'user__last_name']`

  • ModelAdmin
  • admin_order_field
  • multifiled sorting
Read More

2955 snippets posted so far.