***About***
I tried to dump data from my database (manage.py dumpdata) and I couldn't do it because of error:
User matching query does not exists
I found out that my database was filled with garbage: entries those foreigners were deleted. My table's engine is MyISAM so it allows for these lost entries to exist. I had to cleanup my database before I do datadump, so I've written a script which worked fine for me.
***Usage***
Place this script in same directory with your settings.py and run it: `python db_cleanup.py`
***Disclaimer***
Backup your data :)
A Widget for displaying ManyToMany ids in the "raw_id" interface rather than in a <select multiple> box. Display user-friendly value like the ForeignKeyRawId widget
This is custom tag I wrote for myself for solving situations when you have filter form and page
numbers in the same page. You want to change ?page=.. or add it if it doesn't exist to save
filter form data while moving through pages.
Usage: place this code in your application_dir/templatetags/add_url_parameter.py
In template:
{% load add_get_parameter %}
<a href="{% add_get_paramater param1='const_value',param2=variable_in_context %}">
Link with modified params
</a>
It's required that you have 'django.core.context_processors.request' in TEMPLATE_CONTEXT_PROCESSORS
URL: [http://django.mar.lt/2010/07/add-get-parameter-tag.html](http://django.mar.lt/2010/07/add-get-parameter-tag.html)
Django JSON view decorator. Dumps the object returned from the view function. Allows you to customize the JSON dumps() function using the available keyword arguments available from the simplejson module. By default, indents the output with 4 characters.
This module provides a middleware that implements a mechanism to
highlight a link pointing to the current URL.
Every link on the rendered page matching the current URL will be highlighted with a 'current_page' CSS class.
The name of the CSS class can be changed by setting `CURRENT_PAGE_CLASS` in the project settings.
Originally done by Martin Pieuchot and Bruno Renié, thanks @davidbgk and @samueladam for improvements & optimizations.
Automatically register models for the Django-admin.
This snippet aids in the process of keeping your admin.py up-to-date with the models in your apps, have all models automatically added to the admin and reflect any future changes without updating the file.
[zweckdev.com](http://zweckdev.com/)
Widget for editing CommaSeparatedIntegerField with a set of checkboxes. Each checkbox corresponds to single integer value. So, choices should be pre-defined for widget.
**Example**
Assume, we need a registration form for some event with time frame for 3 days, starting at Monday. User can select any of 3 days, so we need to show 3 checkboxes.
Here's the basic example for such form:
class EventRegistrationForm(forms.Form):
days = forms.CharField(widget=NumbersSelectionWidget(
['Mon', 'Tue', 'Wed'], range(1, 4)))
I needed to use class based views, but I wanted to be able to use the full name of the class in my URLconf without always having to instantiate the view class before using it. What helped me was a surprisingly simple metaclass.
I can now both instantiate view classes and use the instances as view functions, OR I can simply point my URLconf to my class and have the metaclass instantiate (and call) the view class for me. This works by checking the first argument to `__call__` – if it's a `HttpRequest`, it must be an actual HTTP request because it would be nonsense to attept to instantiate a view class with an `HttpRequest` instance.
The `View` base class contains a overridable before method that can be used to add a common procedure to handlers of different HTTP requests. The `before` method can modify the args and kwargs of the request handlers to be able to replace, say, `model_id` with `model`.
An example of how to select the "default" database based on the request URL instead of the model. The basic idea is that the middleware `process_view` (or `process_request`) function sets some context from the URL into thread local storage, and `process_response` deletes it. In between, any database operation will call the router, which checks for this context and returns an appropriate database alias.
In this snippet, it's assumed that any view in the system with a `cfg` keyword argument passed to it from the urlconf may be routed to a separate database. Take this urlconf for example:
`url( r'^(?P<cfg>\w+)/account/$', 'views.account' )`
The middleware and router will select a database whose alias is `<cfg>`, or "default" if none is listed in `settings.DATABASES`, all completely transparent to the view itself.
Often I want to call a custom manager method in the template, something like Snippet.objects.get_latest(). I hate writing custom templatetags to do all that work, so instead I've written a filter that will work for any. Here's how I use it:
{% for snippet in "cab.snippet"|call_manager:"top_rated"|slice:":5" %}
I often find myself testing that the queryset returned by a method contains the instances I expect. I use a custom method, **assertQuerysetEqual()**, to test the equality of two querysets or lists::
def test_some_values(self):
qs = get_user_list()
self.assertQuerysetEqual(qs, [normal_user, super_user])
Makes it easy to test small querysets against lists whose values are known and expected.
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.