Modelling¶
Automatic created and modified timestamps¶
from django_extensions.db.models import TimeStampedModel
class MyModel(TimeStampedModel)
Tree-based models¶
get ancestors / get_descendants
mptt_model.get_ancestors()
mptt_model.get_descendants()
Get UML for models¶
django-admin.py graph_models app -o app.png
Introspection¶
Get all attributes
User._meta.fields()
Check attribute type
isinstance(field, models.ForeignKey)
Where is the foreign key pointing to?
field.rel.to
More on http://www.b-list.org/weblog/2007/nov/04/working-models/
Filter dynamically
search_column = "name_" + lang
search_value = self.kwargs.get("category")
Category.objects.filter(**{ search_column: search_value })