Showing posts with label django. Show all posts
Showing posts with label django. Show all posts

Monday, January 30, 2017

Caching for anonymous (non-authenticated) users in Django

Hi boys and girls,

Recently I was optimizing performance on some of my django sites and needed to cache all views for anonymous users, but still render for authenticated users. Django documentation is silent on this. I've also checked out stackoverflow, but people there recommend using templates, which I don't like. So below I will give you my version of how to solve that.

Saturday, January 21, 2017

Django Haystack - how to limit number of search results

Hello friends,

Recently I needed to limit number of search results in haystack and that was a bit of a challenge. So I decided to share it with you here.

I knew how to limit search results using old style views (haystack.views.SearchView):
from haystack.views import SearchView

class MySearchView(SearchView):

    #limit maximum results
    def get_results(self):
        return self.form.search()[:100]

Saturday, November 19, 2016

Migration from MySQL to Postgresql with Django is easy as 1-2-3

Hi guys,

Short story: I started learning Django few years ago and created my first django website using mysql database. Over time I realized how much more powerful Postgresql is. And yesterday I decided to migrate my website from mysql to posgresql. But the main question - how?

First google search gave me suggestion - dump mysql database, then convert it to posgresql compatible format using some scripts. Check this page if you're interested.

I started doing it, but there were always some errors, even after conversion. For example, this:
ERROR:  column "blabla" is of type boolean but expression is of type integer

Wednesday, November 16, 2016

django-cron running command constantly instead of on schedule

Hi python-lovers,

Today I've experienced weird problem with django-cron==0.4.6 (and django 1.9.5). And I thought it would be useful to share with you.

I had job class which is defined like this:
class MyJob(CronJobBase):
   RUN_EVERY_MINS = 60 * 24    
   schedule = Schedule(run_every_mins=RUN_EVERY_MINS)
   code = 'myapp.myjob'    
   def do(self):
       do_some_task()

Theoretically it should be run once a day. But in practice, every time when I was trying to run "python manage.py runcrons", do_some_task() had been called.

I tried everything, including downgrading django-cron. But the actual problem was that do_some_task had an exception in it. Unfortunately it was not reported in any way. No logs, no warnings were spit by django-cron.

The conclusion

If there is any exception in your job, it will be re-run unlimited number of times by django-cron, ignoring your schedule. Beware of that.

Saturday, February 20, 2016

Django prefetch_related VS select_related in a single database query

Imagine the following model:
class Parent(Model):
    ...

class Child(Model)
    parent = ForeignKey(Parent)
    ...

I want to list all parents along with first related child.

Friday, November 6, 2015

How to setup Shibboleth Identity Provider 3 with Django Website

Recently I was given a task to setup Shibboleth SSO to integrate different python websites with single sign on. There is official shibboleth documentation, but it's quite difficult to understand, especially for beginners. After spending days of googling and reading different forums, I decided to create step-by-step guide for beginners on how to install Shibboleth SSO with django website.

Wednesday, October 21, 2015

Using django models in standalone application

One of my favorite things about Django is its loosely coupled design. It means that you can use each part independently and not affect others. And one of the parts that I often use in my applications is Djagno Models.

There is nothing difficult to use django models in standalone application. You just need to start new django project and then remove all unnecessary stuff leaving only models. Below I will show it on example.