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

One comment

  • 1
    July 24, 2015 - 10:38 am | Permalink

    check your batteries first! usalluy problems start when we have almost zero life batteries..lol.bring it to the nearest service center to be safe! dont ever try troubleshooting when you have no idea at all on how to do it properly..

  • Leave a Reply

    Your email address will not be published. Required fields are marked *