Tag Archives: code

>Django OR statements / conditions

>

I’m having major problems focusing on one thing at a time at the moment. I thought that being in Japan would help me but it appears not. I’ve made some changes to how I work recently and I’m going to have a go at documenting what I’m doing with code and sysadmin stuff here. Partly as a brain dump for when I can’t remember how I did stuff and partly to see if it makes it stick in my brain. Past experience shows this won’t be something I keep up by anyway. Today I’m plodding through writing some Django code which is a rewrite of some pure python/sql stuff I did before. I know a lot of sql and therefore am having to force myself to try and use the Django API efficiently as much as possible (partly so I know when I shouldn’t use it). Todays question is how do I do OR statements.

Well according to the docs you setup Q objects. OK … I’ll stay calm. Here’s my current code.

    usages = Usage.objects.filter(parttype=parttypeid)
usages = usages.filter(client=self.client)

Just to point out that I’m doing two conditions here which are AND’ed together. Firstly to find any Usage record with a parttype of whatever parttypeid is and secondly to find any client which is the same as our client record. I could have given the first condition a PartType record or I could have given the second condition an id instead of a Client object. Although these two conditions are given in different lines they won’t be evaluated until I try and access the records at which point just one query will be thrown at the database. I also want to do an OR statement so that whatever is found in the above two statements much match. This is where I must use my Q object. Import it from django.db.models.Q

    Q(dockcode__productionline__destination=destinationid)

That looks like a long fieldname. Actually it’s some black magic that Django does for working across relationships. dockcode is a related table to usage, productionline is related to the dockcode table and destination is the actual destination we want to get to. They are double underscores to show you are referring to a related table. I don’t like the syntax but I can live with it for the moment. OK – but we want to look for two possible destination ids. So we end up with this

   qs = ( Q(dockcode__productionline__destination=destinationidA)|                  
Q(dockcode__productionline__destination=destinationidB))

And we just add that to our filter which we haven’t actually executed yet. So in total we end up with

    usages = Usage.objects.filter(parttype=parttypeid)
usages = usages.filter(client=self.client)
qs = ( Q(dockcode__productionline__destination=destinationidA)|
Q(dockcode__productionline__destination=destinationidB))
usages = usages.filter(qs)

That will return any usage record which has the given parttype, the given client and one of two possible destination ids. Well it will – when you try and access the result.

>Django Settings File

>Whilst I love Django, there’s a number of odd things about it which make me puzzled. One of those is the settings file. When you have a team of programmers it’s important to be able to check each others code out with the minimum of fuss and to be able to deploy that code live in the same fashion.

Thus it was that the settings.py file used within Django has always puzzled me. You specify directories for where your templates go and you’ll see the following


TEMPLATE_DIRS = (
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
)

I have no idea why this does this, and I’m not the only person but as I’m as thick as a whale omelette I am not going to argue with the developers. The real ‘wtf’ is why the comments lead people to put paths in that will break when used on the next developer’s machine. I’m developing on linux in my home directory, we deploy onto linux into a different users directory, the other developers mostly use Macs which have a different directory structure for homes.

Anyway – enough. As the ticket comments for this ‘fault’ say. The solution is simple. settings.py is a python file so do something like this.


import os
root_path=os.path.realpath(os.curdir)
TEMPLATE_DIRS = (
"%s/legacy/templates" % root_path,
"%s/edi/templates" % root_path,
)

And your world will be a better place.