Login

All snippets

Snippet List

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

LocaleMiddleware without browser language discovery

This snippet holds your Django project from automatically changing language of the page to the best fitting one by discovering the client browser language. I personally needed to show the page to the user for the first time in the default language (English), although there were some translations. User can still change the language (via session cookies). Insert this middleware BEFORE the Django's `django.middleware.locale.LocaleMiddleware` in settings.

  • middleware
  • i18n
  • locale
Read More

Seeded Randomized Querysets w/ Pagination Mixin

Mixin to support pagination when randomizing querysets. Requirements: Postgres, Django Sessions Note: This shouldn't be used on large complex datasets. It utilizes the relatively slow method of '?' randomized sorting. Use with caution. Todo: MySQL support, Support for larger datasets

  • django
  • session
  • pagination
  • random
  • postgres
  • mixin
  • postgresql
  • cbv
  • seeded
Read More

Load dynamically loaded form javascript assets using dajax

I am used to load forms directly into modals using dajax but I found out I had to load the scripts using an ajax call from the browser. You can see here an example of a dynamically loaded form and the function used to load the scripts.

  • ajax
  • javascript
  • forms
  • script
  • dynamically-loaded-form
  • form-assets
  • dajax
Read More

Decorator @not_login_required

This is a simple django snippet! It is the *opposite of @login_required* decorator for Django views Example **@not_login_required** ``` def login_page(request): ... ```

  • decorator
Read More

get_querystring template tag

A Django Template tag used to construct urls with current querystring parameters. This is based on some code that I've written some years ago. Enjoy.

  • template
  • templatetag
  • querystring
Read More

3109 snippets posted so far.