Search with OR in Django

0

Hello, I'm developing my 1st application in Django and I'm having difficulty with more complex DB queries.

Here's my MODEL:

class Tag(models.Model):

    objects = GChartsManager()

    idtag = models.IntegerField(db_column='idTAG', primary_key=True, editable=False)  # Field name made lowercase.
    desvio = models.FloatField(db_column='DEVIATION', blank=True, null=True)  # Field name made lowercase.
    tempo_max = models.IntegerField(db_column='TIME_MAX', blank=True, null=True)  # Field name made lowercase.
    conv_rate = models.IntegerField(db_column='CONV_RATE', blank=True, null=True)  # Field name made lowercase.
    taginfo_idtaginfo1 = models.ForeignKey('Taginfo', db_column='tagInfo_idTAGINFO')  # Field name made lowercase.
    datasource_idestacao_meteo = models.ForeignKey(Datasource, db_column='datasource_idDATASOURCE', blank=True, null=True)  # Field name made lowercase.

    def __unicode__(self):
        return u'id = %s / id tagInfo => %s' % (self.idtag, self.taginfo_idtaginfo1)

    class Meta:
        managed = False
        db_table = 'tag'


class Taginfo(models.Model):

    objects = GChartsManager()

    idtaginfo = models.IntegerField(db_column='idTAGINFO', primary_key=True, editable=False)  # Field name made lowercase.
    nome = models.CharField(db_column='NAME', max_length=45)  # Field name made lowercase.
    descricao = models.CharField(db_column='DESCRIPTION', max_length=255, blank=True)  # Field name made lowercase.

    def __unicode__(self):
        return u'name = %s / description = %s' % (self.nome, self.descricao)

    class Meta:
        managed = False
        db_table = 'taginfo'

And I want to do a search finding all Tag's of a various taginfo's, ie the tags variable is actually several objects, always:

tagInfos = Taginfo.objects.filter()
        for tag in tags:
            tagInfos = tagInfos.filter(idtaginfo = tag.taginfo_idtaginfo1.idtaginfo)

But this search does not return anything to me. I've already asked this question and they replied that this would work or use Q objects, Objects Q but I did not understand how I would do this in my case where the quantity of items is variable, from 0 to n. Thanks in advance for the help

    
asked by anonymous 17.06.2015 / 00:07

1 answer

3

If you want to get all tags of a TagInfo is a simple search, since you have a ForeignKey in Tag

tags = Tag.objects.filter(taginfo_idtaginfo1__in=[LIST_OF_IDS])

    
17.06.2015 / 01:24