Error searching in Django

Asked

Viewed 154 times

1

Hello I am creating a project on Jango and performing a search for a Taginfo based on a tag that has foreignkey for Taginfo. I’m getting the following error on line 99 from view.

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

Here is the model of the 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 pq this error occurs pq the both fields are int and one is Foreign key and the other a Primary key.

Thank you in advance.

  • 1

    What is the 99 line of the view?

  • Line 99 -> tagInfos = tagInfos.filter(idtaginfo = idTaginfo)

  • That’s what I figured. In this case, see my answer, if you still have questions you can ask.

1 answer

1

On the line:

idTaginfo = tag.taginfo_idtaginfo1

You should use:

idTaginfo = tag.taginfo_idtaginfo1.idtaginfo

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

tagInfos = tagInfos.filter(idtaginfo = idTaginfo) # idtaginfo é um IntegerField
  • I put it the way you spoke in the code código

  • I put it the way you said it in the code idTaginfo = tag.taginfo_idtaginfo1_id and also idTaginfo = tag.taginfo_idtaginfo1.idtaginfo &#Both give no error but do not return any search in the BD, when testing by Django shell I received the error " `...has in the attribute ' taginfo_idtaginfo1_id ' "

  • @Nathan Ops, sorry, I didn’t realize your model TagInfo had a custom primary key (i.e. it is not called id). I updated the answer, it should work now. If still do not solve, tomorrow I will do some tests and I return to that question.

  • thanks for the tip, it does not generate error but also does not return me any item,kkk. In the shell an error occurs when using tag = Tag.objects.filter(taginfo_idtaginfo1.idtaginfo = 2) the error generated is keyword can´t be an expression but when using the &#Xa code;tag = Tag.objects.filter(taginfo_idtaginfo1__idtaginfo = 2) an object is returned, which is what should occur. I saw this form of search on the link [https://django-portuguese.readthedocs.org/en/1.0/topics/db/queries.html#shortcut-to-pk]

  • @ mgibsonbr however this same code generates error in the project, the error is 'Tag' object has no attribute 'taginfo_idtaginfo1__idtaginfo' I didn’t understand how shell can work and project can’t.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.