Get derived model without storing their names or content types in databases. You write only one line, it expands into only one SQL-query (with many LEFT OUTER JOIN's).
Model definition example:
class BaseModel(models.Model):
foo = models.IntegerField(null=True)
derived = DerivedManager()
class FirstChild(BaseModel):
bar = models.IntegerField(null=True)
class SecondChild(BaseModel):
baz = models.IntegerField(null=True)
How to use:
>>> f = FirstChild.objects.create()
>>> s = SecondChild.objects.create()
>>> print list(BaseModel.objects.all()
[<BaseModel object 1>, <BaseModel object 2>]
>>> print list(BaseModel.derived.all()
[<FirstChild object 1>, <SecondChild object 2>]
>>> print BaseModel.derived.get(pk=s.pk)
<SecondChild object 2>
A simple example to show how to manage an history of memo-on-save.
Each time the user saves the model, he must provide a memo message.
The full memos history is shown as a read-only tabular-inline.
For each memo, user and date are also registered
([Django-Admin-Collapsed-Inlines](https://github.com/virajkanwade/Django-Admin-Collapsed-Inlines) could be usefull)
This is another fork of http://djangosnippets.org/snippets/2729/ that fixes the issue.
Unlike those other versions i give you instructions so it works for you, this is modified a little.
Instructions:
If you want to import the passwords from drupal you need to prepend to each of them the word drupal so it looks like this:
drupal$S$DQjyXl0F7gupCqleCuraCkQJTzC3qAourXB7LvpOHKx0YAfihiPC
Then add this snippet to someapp/hashers/DrupalPasswordHasher.py
And then in your settings add this:
PASSWORD_HASHERS = (
'django.contrib.auth.hashers.PBKDF2PasswordHasher',
'someapp.hashers.DrupalPasswordHasher',
'django.contrib.auth.hashers.PBKDF2SHA1PasswordHasher',
'django.contrib.auth.hashers.BCryptPasswordHasher',
'django.contrib.auth.hashers.SHA1PasswordHasher',
'django.contrib.auth.hashers.MD5PasswordHasher',
'django.contrib.auth.hashers.CryptPasswordHasher',
)
So, what did i modify
First i added the attribute algorithm to the class, django uses this word to identify wich hasher to use, so the passwords beggining with drupal should be verified with the hasher.algorithm == 'drupal'
Ok so know django knows to use our class, but now our passwords won't validate because we changed them by adding the word drupal, so what do we do? we modify the verify method to remove the word drupal before verification :P
Hope it helps
A hack to add __in ability to links generated in the Django Admin Filter which will add and remove values instead of only allowing to filter a single value per field. Example ?age_group__in=under25%2C25-35
When work at site api for android client, I found use form to validate user input is too complex, so I write this.
usage:
@param('param_name', 'default_value', validate_func)
def api_func(request):
# access cleaned data.
param_name = request.DATA['param_name']
JsonResponse is my class. replace with HttpResponse or whatever
This snippet is an extension of [i18n base model for translatable content](http://djangosnippets.org/snippets/855/) so all the same usage applies.
I have extended this module in several ways to make it more fully featured.
* `I18NMixin` can be an additional (via multiple inheritance) or alternative superclass for your models juxtaposed with an `I18NModel`.
* Adds a property `_` to access the appropriate I18NModel. `trans` aliases this (or rather vice versa) for template access.
* In a call to `.filter` you can query on translated fields by wrapping those fields in a call to i18nQ. I like to import this as _ if I haven't already used that import.
* A call to I18NFieldset will return an inline for use in the builtin admin app. I like to call this inline to the assignment to inlines.
* If you need abstracted access to the I18N model from a model, I've added a property I18N referring to it.
I've been using this with great convenience and stability.
In Django 1.5 url tags require you to pass in the name of the url as a string.
So where you used to be able to do this {% url home_page %} you now have to do this {% url 'home_page' %}
Upgrading an old project can be a pain, so here is a snippet for a py file that will update all your url tags.
Just put it in a py file in your root directory and execute it.
The error you get otherwise is:
'url' requires a non-empty first argument. The syntax changed in Django 1.5, see the docs.
This file includes two Django view decorators `header` and `headers` that provide an easy way to set response headers.
Also, because I have to work with a lot of cross domain requests, I include few shortcuts for convenience to set the Access-Control-Allow-Origin header appropriately.
You're looking at the most-bookmarked snippets on the site; if you'd like to help useful snippets show up here, sign up for an account and you'll get your own bookmarks list.