Example Django Model Count and Group By Query
The Django framework abstracts the nitty gritty details of SQL with its Model library (the M in MVC). It is straight forward for standard lookups involving WHERE clauses. 10% of the time I need it to...
View ArticleDjango Automatic Slug Generator Abstract Class
Using Django, an example of how to auto populate a slug field based on the name or title of whatever the thing is. Correctly handles duplicate values (slugs are unique), and truncates slug if value too...
View ArticleDjango enable SQL debug logging in shell how to
How to get Django to show debug SQL logging output in the shell. Works in Django 1.10! Start the Django shell: python manage.py shell Paste this into your shell: import logging log =...
View ArticleDjango Group By Having Query Example
Example Group By Having query in Django 1.10. Let’s say you have SQL such as: SELECT DISTINCT user_id, COUNT(*) FROM my_model WHERE tag_id = 15 OR tag_id = 17 GROUP BY user_id HAVING COUNT(*) > 1...
View ArticleDjango – Correctly Wiring to AWS CloudFront for Static and Media Files
When it comes to “How To Setup a CDN for Django” most people suggest the following “half way” setup that can lead to stale cache problems and slowness in the admin. Configure Django to upload media and...
View ArticleDjango Tricks for Processing and Storing JSON
In this post I’ll show a few tricks I use to make JSON fit into Django more seamlessly. The first is a lesson on coding that everybody should know. Parsing external JSON: Whenever you take in JSON from...
View ArticleTips and Tools for Securing Django
Django has a number of built in security measures which are really helpful. In fact as frameworks go by default it does a lot of really smart things security wise. There are a few less commonly known...
View ArticleMaking Django’s Database Connection More Secure for Migrations
Since Django 1.7 (September, 2014) and the introduction of schema migrations, it has always bugged me that Django needs to connect to the database with a user that has pretty close to ALL privileges....
View ArticleDjango Rest Framework How To Whitelist (Safelist) IP Addresses
Here is how to setup a list of IP addresses / subnets that are allowed to call a Django Rest Framework endpoint. All other IP addresses will be blocked. Using an IP safe list is much easier than...
View ArticleQuerying Complex Data in Django with Views
Django has a powerful ORM, but like any ORM it suffers when querying data that has complex relationships. Django supports foreign keys and many to many relationships out of the box with ease. Sometimes...
View Article
More Pages to Explore .....