Login

Top-rated snippets

Snippet List

Sophisticated order_by sorting

I wanted to sort a CharField which consists of digits in a different way. This field is a matricle number field (some kind of registration number for students. They have matricle numbers in the format YYxxxxxx - which means "YY" are the last two digits of the year they started studying.) So I wanted to sort them in a way that they appear like this: 5000000, 5000001, ... , 9999998, 9999999, 0000000, 0000001, ... , 1200000, ... , 4999999 Took me some time to find out how to do this efficiently in PostgreSQL, and so I thought I'd share it here. The important stuff is in the model "Candidate" to use a special "objects" object manager which uses a special QuerySet as well. Here lies the "magic": If there is a ordering required that contains "mnr", then a special on-the-fly calculated field will be added to the queryset called "mnr_specialsorted". Now it is possible to do things like `Candidate.objects.filter( firstname__contains="Pony" ).exclude( lastname__contains="Java" ).order_by("lastname", "-mnr")` For other database engines you might want to change the MNR_SORTER variable to fit your needs.

  • sort
  • orderby
  • sorting
  • ordering
Read More

Django admin inline ordering - javascript only implementation

Having spent ages trying out various admin inline ordering packages and examples I found on here and elsewhere I failed to find a single one that did what I was after in the way I wanted or that worked, so I wrote one! The general idea for this version was to be done purely in javascript, no additional methods or parameters required on your models, it's designed to be stuck in a js file and included in your admin class Media js parameter: class Media: js = ['js/admin/widget_ordering.js', ] Your model should have an integer column for sorting on, the name of this column should go in the 'sort_column' parameter at line 3 and your model should also obviously specify this in it's Meta 'ordering' class: class Meta: ordering = ('rank',) That's it! This is a pretty basic implementation that adds simple up and down buttons next to the sort order field, if you want to adapt this to use drag and drop or something, please feel free!

  • admin
  • sorting
  • ordering
  • inline
  • tabular-inlines
Read More

Trim the center of a string

I had to build unique strings for a payment system and i wanted to make them kindof friendly so i generated them with usernames and datetimes(safe enough uniqueness in combo), some usernames are long and they break the limit of this payment system so i thought i should cut the center of the string so it stills has a part of the username and a part of the datetime, the most changing part of the datetime is of course the last part, as microseconds vary rapidly. So i wrote this little function to cut the center of a string i thought it cute so i leave it here. Pay attention to the comment so you can see what is going on.

  • python
  • string
  • slice
Read More

Use django-social-auth & Google Accounts for admin login

1. Create an app and place this in `admin.py`. 2. Add `url(r'^login/$', 'social_auth.views.auth', {'backend': 'google'}, name='login')` to your `urls.py`. 3. Add the app to your `INSTALLED_APPS` after `django.contrib.admin`. 4. Set `USE_SOCIAL_AUTH_AS_ADMIN_LOGIN = True` in your `settings.py`. 5. ... 6. Profit.

  • admin
  • login
  • auth
  • google
  • openid
  • django-social-auth
Read More

QRCode template tag

I had a hell of a time getting most QR code stuffs out there working with Django. Many projects, but problems here and problems there. The only additional code you'll need for this is to "pip install qrcode". After that, you're free to <img src="{% qrcode_datauri "http://localhost:8000/" %}" />.

  • qrcode
Read More

Expand(flatten) url patterns

This is a small function for those time when you want a list of all your urls, expanding included urls, so in the end is like all your urls are in one module. This function recursively does it, so it doesnt matter how nested the includes are in the end you get one flat list.

  • urlpatterns
Read More

Function cache decorator

This is caching mechanism augmented to help address a number of common pitfalls in caching: cache stampeding, simultaneous expiry, cache invalidation, and the storing of None as a legitimate value. No doubt the automatic key generation could stand to be simplified, but it works.

  • cache
Read More

Tatsypie: additional list endpoints for custom Model's manager methods

Although configuring filtering in TastyPie is possible, it is limited to per-field filters, which are not enough for more complex filtering. If your model implement custom manager methods for complex filters, exposing these methods as TastyPie Resource list endpoints is not an easy task. The ModelResource subclass provided here does this, providing 3 ways of complex filtering: * define querysets for filters directly in the Resource declaration * use Model manager custom method * use QuerySet custom method

  • managers
  • manager
  • custom-manager
  • tastypie
Read More

Git Revision Template Tag

This displays the short commit id from the current HEAD. Add this to your admin/base_site.html or any other template. {% block userlinks %} /&nbsp;HEAD: <span style="color:yellow">{% git_short_version %}</span> /&nbsp; {{ block.super }} {% endblock %}

  • git
Read More

nbsp filter

Replaces usual spaces in string by non breaking spaces. "some words" --> "some&nbsp;words" Usage in template: {% load nbsp %} .... {{ user.full_name|nbsp }}

  • templatetag
  • nbsp
Read More

3110 snippets posted so far.