Login

Snippets by tomzee

Snippet List

Field map for models

It seems like one way or another I always need to get access to a specific field of a Model object. The current way to do this is to iterate through the object's _meta.fields list, comparing with the `name` attribute. Why not just have a lookup of fields? If you paste this code at the bottom of your models.py file it will add a `field_map` attribute to the meta options. For example: `profile = User.objects.get(id=1).get_profile()` `upload_to = profile._meta.field_map['image_icon'].upload_to` ...

  • models
  • fields
  • meta
Read More

freshdb management command

This is useful especially during the model-creation stage, when things are in constant flux. The `freshdb` command will drop the project's database, then create a new one. A common use case: manage.py freshdb manage.py syncdb

  • db
  • database
  • management
Read More

tomzee has posted 2 snippets.