Category index for “django”
SQLALchemy vs Django ORM
If you are working with Django ORM most of the time and then switching to SQLAlchemy - you may face some unexpected behavior. In this post I’ll try to describe the most important differences from my point of view.
All examples for SQLAlchemy will be shown in async code, in context of PostgreSQL. Django version - 4.2, SQLAlchemy version - 2.0.
The full examples can be found here https://github.com/st4lk/sqlalchemy-vs-django-orm, in the article the code will be cutted to be short.
Trap in counting related objects in Django
Task: for every object count number of related objects satisfying some conditions.
Django: signal or model method?
When I needed to implement some functionality on model saving, I always asked a question to myself - where to place it. In signal or in model method
save()
? Let’s see, what and when is more applicable.Trying JSON in Django and PostgreSQL (and compare with MongoDB)
New JSONField will be added in Django 1.9, it can be used with PostgreSQL >= 9.4. Let’s try to work with it and find out, in what situations it can be useful.
OAuth and django rest framework
This is a well known topic, but i can’t find the existing solution that will fully satisfy me. So i write it by myself :).
Assume we have a “single page” web site, that is talking with backend via REST API. Client side can be written with ember, angularjs or some like this. Backend - django rest framework (DRF). We’ve got a task - add social login (OAuth protocol).
Debug SQL in django test
In django tests we can measure number of sql queries:
def test_home(self): with self.assertNumQueries(1): response = self.client.get('/') self.assertEqual(response.status_code, 200)
If code in context of
assertNumQueries
will make other number of DB attempts than expected (here 1), test will throw error. But when test fails it is sometimes hard to understand, what unexpected query was made. To debug such case very useful to log SQL expressions to console. Below is description how to do it.Django celery setup
To enable celery in new django project i often look in previous ones to refresh in my memory some steps: what settings should be specified, how to launch, how to stop and so on.
Here i want to combine all together in one place.
Nested SQL queries in Django
Did you know, that Django ORM can do nested SQL queries? Shame on me, but i’ve found it not so long ago.
Send email in django project with mandrill service
To send email messages from server we can just use SMTP protocol. But there is another way - special email services. I’ll describe one of them here, mandrill.com.
Django project: from beginning to working server
In this post i’ll describe my experience of deploying django project to VPS-hosting.
Steps of deploying and server setup:
- Creation of django project
- Deploy setup
- Server setup
- Deploy
Django logging settings
Let’s look at default django logging settings and try to make them more useful.
Here what we have in settings.py after command
django-admin.py startproject project_name
(django 1.5):Django queryset.count cache
Once I mentioned, that my django application makes several similiar queries like
SELECT COUNT(*) ...
. As it turns out (for me it was surprise),queryset.count()
has not obvious cache. But let me start the story from the beginning (and sorry for my english :) ).Parse url which contains unicode query, using urlparse.parse_qs
Task: get dictionary of URL GET query. For example, we have following url:
http://example.com/?key=value&a=b
it is needed to get a dict:
{'key': ['value'], 'a': ['b']}
Values are lists, because one key may have several values:
In: http://example.com/?key=value&a=b&a=c Out: {'key': ['value'], 'a': ['b', 'c']}
Django admin site optimisation
It is known, that less amount of database attempts leads to better performance. Usually admin page - part of site with low traffic, but anyway it is good, if no additional database calls happen there. It is more pleasant to use it, because page renders faster and also frees server resources.
In this post i’ll try to reduce some database attempts in admin page, when model method
__unicode__
contains related object field. I suppose, that examples describe the situation more clear.Multilanguage site on django without redirects
Starting from django 1.4, it is possible to set url prefix for each activated language. For example, we want site, that will have russian and english version.
To do it, add following to
settings.py
:MobileESP: Easily detect mobile web site visitors
Script will be useful, if you want to show different version of site for desktop computers and mobile devices. Big variety of methods to detect mobile type. Avaliable in different languages, including python. The port to python was made by me with help from my freelance customer.
Debug django project with embedded python debugger pdb
I use sublime-text as code editor. It doesn’t have a debugger, so to debug django projects i often used
print var_name
and look for output in local development server console. I use this approach today also, but sometimes it is great to run code step by step to see variables at each step.
It can be done with embedded python debugger pdb:
import pdb; pdb.set_trace()