Login

Snippets by jedie

Snippet List

Limit ManyToMany fields in forms

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/

  • forms
  • manytomany
Read More

local django model test

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/

  • model
  • testing
  • test
  • local-test
Read More

FileField with file extension whitelist

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.

  • forms
  • filefield
Read More

change a widget attribute in ModelForm without define the field

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.

  • newforms
  • forms
  • field
  • modelform
Read More

bigger textfields in admin panel

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

  • admin
  • textfield
  • admin-panel
Read More

Trigger a user password change

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.

  • password
  • user-account
Read More

local django test

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 ;)

  • testing
  • test
  • local-test
Read More

jedie has posted 12 snippets.