1

I have two models in django :

class Matching(models.Model):
    representative_remote_id = models.CharField(max_length=200, null=True)
    representative_name = models.CharField(max_length=200)
    representative_group = models.CharField(max_length=200)


class Vote(models.Model):
    representative_name = models.CharField(max_length=200, blank=True, null=True)
    representative_remote_id = models.CharField(max_length=200, blank=True, null=True)
    # ...

As you can see, there are no relation between these two models. What I want is select all votes where :

vote.representative_name = CONCAT(matching.mep_name, ' (', matching.mep_group, ')')
AND matching.representative_remote_id IS NULL
AND vote.representative_remote_id IS NOT NULL

I can’t find a way to do this with django (without using 'raw' sql query)

luxcem
  • 1,807
  • 22
  • 37
  • 1
    make your join on rep_name = concat(match.mep_name etc...) and put your matching.rep_rem_id is null and vote.rep_rem_id is not null in your where clause --------- It's not that you have no relation it's that your relation is split in multiple fields in the second table if you use "=" anywhere between 2 tables and there is a result, that is a relation – Jeremy C. May 22 '15 at 13:39
  • Thank you Jeremy C, but I don’t see how to do that, could you provide a small code example ? – luxcem May 22 '15 at 13:45
  • would you mind if it's straight up sql and could you provide me with some data from your tables? I'll create a sqlfiddle to explain what I mean, that is if I can get this done within 45 minutes because that's when I need to leave – Jeremy C. May 22 '15 at 13:46
  • Hum, I know the sql request but I’d like to do it with django database functions. – luxcem May 22 '15 at 13:47
  • I'm going to have to disappoint you then because I don't really have any experience even with python in general – Jeremy C. May 22 '15 at 13:48
  • Maybe this might help though:https://docs.djangoproject.com/en/dev/ref/models/querysets/#raw Appearantly you can run straight up sql in django and still run through the Queryset afterwards – Jeremy C. May 22 '15 at 13:52

1 Answers1

2

I think you could get all the Matching objects with a filter applied on them, then get the Vote objects using that results.

Something like:

representative_names = ["{name} ({group})".format(name=field[0], group=field[1]) for field in Matching.objects.filter(representative_remote_id=None).values_list('representative_name', 'representative_group')]
Vote.objects.exclude(representative_remote_id=None).filter(representative_name__in=representative_names)
mrnfrancesco
  • 1,017
  • 8
  • 26
  • That's two SQL queries, it could be done in SQL with only one query. I don’t know if is it possible with django… – luxcem May 22 '15 at 13:49
  • 3
    http://stackoverflow.com/questions/252976/full-outer-join-in-django Django doesn't support "joins" in the usual SQL sense -- it supports object navigation. Note that a relational join (inner or outer) creates a new "class" of entities. One that doesn't have a definition in Django. So there's no proper "result set" since there's no class definition for the things you get back. – mrnfrancesco May 22 '15 at 14:03