GenericRelations with Django to create unique generic foreign keys between objects, it can become tiresome having to navigate the
RelatedManager every time you need to retrieve the single instance of the one-to-one relationship. This post shows how creating a quick
Mixin can help overcome this inconvenience.
Django’s annotate method can be useful for counting particular attributes on every member of a queryset (i.e. count the number of books an author has). Unfortunately you can’t use the ORM to do any filtering on those attribute. This would be useful for only considering an attribute if it were above a certain value, or belonged to a certain object (i.e. count the number of books with the tag ‘Horror’). This post shows how to make use of the
extra method Django provides to create filtered annotations
When beginning with Django I always found it difficult to decide on basic project layouts. What should go where and why? Having read many of the great articles on the topic already out there this post outlines how I have come to manage the folder structure of most of my new Django projects, making sure everything is tidy and safe
When the generated URLs of your objects depends on a field in your model that could potentially change (for example a slug field that is generated from a title which your editor just changed), you can get yourself into trouble with Google as your old URL will now return a 404. This posts outlines an approach to dynamically create redirects whenever an objects changes an url dependant field.
If you are checking your settings.py into Git/Mercurial etc. make sure you aren’t including any potentially sensitive information such as database passwords, secret keys and so on. A quick and easy way to avoid this is to create a separate
At times you need to use both
easy_thumbnails in the same template. Both of these apps define a
thumbnail template tag from a
thumbnail template module meaning that you will be able to use one or the other but not both. To get around this, you can install a great app called django-smart-load-tag
select_related QuerySet method is a great way to reduce the query count when joining tables but it should come with small print in the documentation. This post goes over that small print.