Category index for “django”

  • SQLALchemy vs Django ORM

    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.

    Comments Read More
  • Trap in counting related objects in Django

    Trap in counting related objects in Django

    Task: for every object count number of related objects satisfying some conditions.

    Comments Read More
  • Django: signal or model method?

    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.

    Comments Read More
  • Trying JSON in Django and PostgreSQL (and compare with MongoDB)

    Trying JSON in Django and PostgreSQL

    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.

    Comments Read More
  • OAuth and django rest framework

    Star

    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).

    Comments Read More
  • Debug SQL in django test

    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.

    Comments Read More
  • Django celery setup

    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.

    Comments Read More
  • Nested SQL queries in Django

    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.

    Comments Read More
  • Send email in django project with mandrill service

    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.

    Comments Read More
  • 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:

    1. Creation of django project
    2. Deploy setup
    3. Server setup
    4. Deploy
    Comments Read More
  • 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):

    Comments Read More
  • Django queryset.count cache

    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 :) ).

    Comments Read More
  • 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']}
    
    Comments Read More
  • Django admin site optimisation

    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.

    Comments Read More
  • Multilanguage site on django without redirects

    Star

    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:

    Comments Read More
  • 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.

    Comments Read More
  • 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()
    
    Comments Read More