Error in django search

1

Hello, I'm creating a project in django and doing a search for a Taginfo based on a tag that has a foreignkey for Taginfo. I'm getting the following error at line 99 of the view.

TypeError at /datasources/get/1/
int() argument must be a string or a number, not 'Taginfo'

Here is the model of related classes.

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'

Here is the view where the error occurs.

def datasource(request, datasource_id = 1):
# if there is
if Datasource.objects.filter(idestacao_meteo=datasource_id).count()>0 :
    result = Datasource.objects.get(idestacao_meteo=datasource_id)
    #if there is tags on this datasource
    tags = None
    tagsInfos = None
    if Tag.objects.filter(datasource_idestacao_meteo = datasource_id).count>0:
        tags = Tag.objects.filter(datasource_idestacao_meteo = datasource_id)
        tagInfos = Taginfo.objects.filter()         
        for tag in tags:
            idTaginfo = tag.taginfo_idtaginfo1
            tagInfos = tagInfos.filter(idtaginfo = idTaginfo)
        '''
        tagInfo = {'' : 0} 
        for tag in tags:
            tagInfo[tag.idtag] = Taginfo.objects.get(idtaginfo = tag.taginfo_idtaginfo1)
        '''
    else:
        tags = None
        tagsInfos = None        
    return render_to_response('Datasource/datasource.html',
        #{'datasource' : result, 'tags' : tags}
        {'datasource' : result, 'tags' : tags, 'tagInfos' : tagInfos}
    )
else:
    return render_to_response('Datasource/datasourceDoesNotExist.html',
        {'datasource_id' : datasource_id }
    )       

I do not know why this error occurs because both fields are int and one is foreign key and the other is a primary key.

Thank you in advance.

    
asked by anonymous 10.06.2015 / 23:12

1 answer

1

On line:

idTaginfo = tag.taginfo_idtaginfo1

You should use:

idTaginfo = tag.taginfo_idtaginfo1.idtaginfo

Although in the taginfo_idtaginfo1 model it is a foreign key (and in BD it is represented by a number), in the model instances it is a complex object and can not be used to filter a field that expects a number:

tagInfos = tagInfos.filter(idtaginfo = idTaginfo) # idtaginfo é um IntegerField
    
10.06.2015 / 23:32