background preloader

Django_patterns

Facebook Twitter

Django, paths and URLs « Occasionally sane. Michael Trier has a blog post on how he keeps his Django projects portable from one directory to another. This is something that gave us a hard time at work too and that we managed to fix (differently.) To give you a brief idea of what we do, we usually develop small web sites for small companies. Because of the pains of using PHP, we moved all our new development to Django and have been satisfied.

There are some areas that have caused us trouble however, and the number one pain is deployment. Some things are out of our hands, such as restarting Apache when a Python file is modified. We have a basic Django project skeleton that we now use whenever we start a new site. Our web sites usually go through four different file system paths: My working copyThe working copy of my designerOur demo serverThe production server We also needed our projects to work with different base URLs: Paths The first problem we concentrated on were the file system paths. URLs Our next problem were the URLs. Django Patterns: Model Inheritance. This post discusses the two flavors of model inheritance supported by Django, some of their use-cases as well as some potential gotchas.

Overview When the queryset refactor landed a couple years ago Django's ORM grew support for model inheritance. Model inheritance comes in two flavors, abstract and ... not abstract. This is a bit like the "has-a/is-a" distinction sometimes talked about in object-oriented programming. There are some important differences in how Django handles these two types of inheritance. Multi-table inheritance (not abstract) Directly extending a model results in two tables where the shared fields are stored in one table (the parent model's table) and the fields unique to the child model are stored on the child model's table.

Because of the way these items are stored in the database, it is possible to query against all media objects, whether they're photos, videos, or just plain-old "media" objects. I find MTI useful in the following circumstances: Conclusion Links. Powerful Generic Patterns With Django. Django Coding Patterns. Rosemanblog - patterns. Django-selectreverse - Control your querycount for m2m and reverse FK lookups in django. This module contains a set of helpers to reduce the number of queries needed to access m2m relations or reverse foreign key relations. Introduction Consider a parent-child relationship building-apartements. It is created using a foreign key field to the Building model in the Apartment model. A common pattern is to list some or all buildings and for each of the buildings list the apartments. An example: In the template: In the view: buildinglist = Building.objects.all() The problem is that this causes an extra query for each building, when the list of apartments for each building gets loaded from the database.

Using the ReverseManager in this package, this can be reduced to only one extra query. How ? In the Building model, use the ReverseManager instead (or in addition to) the default manager. objects = ReverseManager() In the view use the select_reverse method to prefetch the appartments: Instead of many queries, this only requires two queries, regardless of the number of buildings in the list. Index — Django Design Patterns.