Converts an integer or floating-point number or a string to a string containing the delimiter character (default comma) after every delimeter_count digits (by default 3 digits)
Django allows you to specify your own ModelManager with custom methods. However, these methods are chainable. That is, if you have a method on your PersonManager caled men(), you can't do this:
Person.objects.filter(birth_date__year=1978).men()
Normally, this isn't a problem, however your app may be written to take advantage of the chainability of querysets. For example, you may have an API method which may return a filtered queryset. You would want to call with_counts() on an already filtered queryset.
In order to overcome this, we want to override django's QuerySet class, and then make the Manager use this custom class.
The only downside is that your functions will not be implemented on the manager itself, so you'd have to call `Person.objects.all().men()` instead of `Person.objects.men()`. To get around this you must also implement the methods on the Manager, which in turn call the custom QuerySet method.
Based on [#2020](http://djangosnippets.org/snippets/2020/)
This snippet creates a simple generic export to csv action that you can specify the fields you want exported and the labels used in the header row for each field. It expands on #2020 by using list comprehensions instead of sets so that you also control the order of the fields as well.
This is in my opinion a better way to have flat pages in a project. In the example with the url patterns settings:
/ will render -> /pages/welcome.html
/contact will render -> /pages/contact.html
/products/ will render -> /pages/products/index.html
/products/pricing will render -> /pages/products/pricing.html
This custom test suite runner will record all of the URLs accessed during your test suite. It will compare it to the list of all URLs you have configured for your site and produce a report of any URLs missed. It requires that all URLs are named (using the ``name=`` parameter).
To use is, set the ``TEST_RUNNER`` variable in your configuration to this class. You can also define ignored URLs. For example, to filter out the admin URLs, you can use:
IGNORED_COVERAGE_URLS = ['^admin/',
'^admin/doc/']
Useful when you want to keep only one instance of a model to be the default one.
This is a decorative way of doing the same as in http://djangosnippets.org/snippets/1830/
Can this be made better as a class decorator (not having to declare explicitly the save method) ?
An improved version of http://djangosnippets.org/snippets/1016/ which lets you add separate buttons for change_list and change_form pages in the admin.
On our site [Fornebuklinikken - A cosmetic surgeon in Norway](http://www.fornebuklinikken.no) we also have a domain [http://fornebuklinikken.com](http://www.fornebuklinikken.no) which should be using the 'en' language.
We didn't wan't to use the standard locale lib, and wrote our own middleware which lookups the correct language corresponding to the domain (.no or .com)
Any questions? Contact me on herman.schistad (at) gmail.com
This is a ModelChoiceField where the choices are rendered in optiongroups
(this is already posible with a normal Choicefield)
For this to work properly the queryset you supply should already be ordered
the way you want (i.e. by the group_by_field first, then any sub-ordering)
I needed a way to find if a menu items should be active. After searching the internet i found a few options*, but none of them did fit my needs, so i wrote my own:
Usage:
<a href="{% url 'view-name' %}" class="{% current request 'view-name' %}"></a>
* http://gnuvince.wordpress.com/2008/03/19/the-new-and-improved-active-tag/
* http://stackoverflow.com/questions/340888/navigation-in-django
This templatetag was inspired by: [Admin App/Model Custom Ordering](http://djangosnippets.org/snippets/1939/).
I rewrote it from scratch because it wasn't working on my install.
This mimicks the keys used internally by the @cache_page decorators and site-wide CacheMiddleware.
Now you can poke them, prod them, delete them, do what you like with them (eg, delete after you update some content and you want a specific URL refreshed).
If your application server is behind a proxy, `request.META["REMOTE_ADDR"]` will likely return the proxy server's IP, not the client's IP. The proxy server will usually provide the client's IP in the `HTTP_X_FORWARDED_FOR` header. This util function checks both headers. I use it behind Amazon's Elastic Load Balancer (ELB).
You're looking at the top-rated snippets currently on the site; if you'd like to contribute, sign up for an account and you'll be able to rate any snippet you see.