Change model field attribute in a DRY way
Change model field attributes from a abstract base classes model in a dry way.
- model
- field
- dry
Change model field attributes from a abstract base classes model in a dry way.
Limit ManyToMany fields in forms. Hide the field, if only one item can be selected. e.g. For limit sites choices only to accessible sites. Also available via django-tools: http://code.google.com/p/django-tools/
unique_together doesn't work with ManyToMany, yet. See: http://code.djangoproject.com/ticket/702 Here a simple test in save() that would raise a IntegrityError.
signal handler for checking unique_together manually. Also available via django-tools: http://code.google.com/p/django-tools/
Limit a view to superuser only. raise PermissionDenied.
Change field meta data or add a html attribute to a ModelForm field in a DRY way.
Sometimes you need to test some model features without a complete django app installation. Just play only with the model object. With these small script you have a complete in memory django installation. Some Links: http://www.djangosnippets.org/snippets/1044/ (en) http://www.jensdiemer.de/permalink/150/mein-blog/99/django-db-model-test/ (de) http://www.python-forum.de/viewtopic.php?f=3&t=15649 (de) See also: https://github.com/readevalprint/mini-django/
A simple FileField with a addition file extension whitelist. Raised ValidationError("Not allowed filetype!") if a filename contains a extension witch is not in the whitelist.
I will change a model form widget attribute without define the complete field. Because many "meta" information are defined in the model (e.g. the help_text) and i don't want to repeat this. I found a solution: Add/change the widget attribute in the __init__, see example code.
Sometimes a textarea field is to small for usage. I add some JavaScript and CSS to resize all textareas a little dynamically: The JS change the size in dependence text-lengthen. You should store this snippet into a file like this: templates_django/admin/base_site.html
I would like to catch the raw plaintext password if a user created or change his password. First i tried to handle this with signals.post_save at the User class, like this: `dispatcher.connect(update, signal=signals.post_save, sender=User)` The problem is, in the User model exists e.g. 'last_login'. So the save method called every time, the user logged in :( And with post_save i get only the hashed password and not the plaintext source password. I found a simple way to trigger a user password change. I hacked directly into the django.contrib.auth.models.User.set_password() method. See the sourcecode. There exists a discussion in the [django-users thread](http://groups.google.com/group/django-users/browse_thread/thread/7c074e80a9cdcd21/) about this.
Sometimes I would like to test a codesnippet without a complete django environment. Here is a small "local test script" you can use for this ;)
jedie has posted 12 snippets.