Justin O Barber
Justin O Barber

Reputation: 11591

ValueError (invalid literal for int() with base 10) in django foreign keys

I'm working with a legacy database and am having some trouble connecting a foreign key in one table to a primary key in another table. I'd be most grateful for any help. Below are the models and my shell+ error. Thanks in advance.

Models:

class SectionMaster(models.Model):
    . . .
    crs_cde = models.ForeignKey('SectionSchedules', max_length=30, db_column=u'CRS_CDE', related_name='sm_crs_cde')
    . . .

class SectionSchedules(models.Model):
    . . .
    crs_cde = models.CharField(max_length=30, db_column=u'CRS_CDE')
    . . .

The error:

>>> q = SectionMaster.objects.filter(trm_cde=20).filter(yr_cde=2012)
>>> q2 = SectionSchedules.objects.filter(trm_cde=20).filter(yr_cde=2012)
>>> for course in q:
...     for course2 in q2:
...         if course.crs_cde == course2.crs_cde: ## also tried course.crs_cde_id
...             print course.crs_cde, course.crs_title, course2.begin_dte
...
Traceback (most recent call last):
  File "<console>", line 3, in <module>
  File "C:\Python27\lib\site-packages\django\db\models\fields\related.py", line
350, in __get__
    rel_obj = qs.get(**params)
  File "C:\Python27\lib\site-packages\django\db\models\query.py", line 358, in g
et
    clone = self.filter(*args, **kwargs)
  File "C:\Python27\lib\site-packages\django\db\models\query.py", line 624, in f
ilter
    return self._filter_or_exclude(False, *args, **kwargs)
  File "C:\Python27\lib\site-packages\django\db\models\query.py", line 642, in _
filter_or_exclude
    clone.query.add_q(Q(*args, **kwargs))
  File "C:\Python27\lib\site-packages\django\db\models\sql\query.py", line 1250,
 in add_q
    can_reuse=used_aliases, force_having=force_having)
  File "C:\Python27\lib\site-packages\django\db\models\sql\query.py", line 1185,
 in add_filter
    connector)
  File "C:\Python27\lib\site-packages\django\db\models\sql\where.py", line 69, i
n add
    value = obj.prepare(lookup_type, value)
  File "C:\Python27\lib\site-packages\django\db\models\sql\where.py", line 320,
in prepare
    return self.field.get_prep_lookup(lookup_type, value)
  File "C:\Python27\lib\site-packages\django\db\models\fields\__init__.py", line
 972, in get_prep_lookup
    return super(IntegerField, self).get_prep_lookup(lookup_type, value)
  File "C:\Python27\lib\site-packages\django\db\models\fields\__init__.py", line
 310, in get_prep_lookup
    return self.get_prep_value(value)
  File "C:\Python27\lib\site-packages\django\db\models\fields\__init__.py", line
 966, in get_prep_value
    return int(value)
ValueError: invalid literal for int() with base 10: '0BJ  226  1
   '

Upvotes: 0

Views: 2614

Answers (2)

Daniel Roseman
Daniel Roseman

Reputation: 599580

I expect you want to_field='crs_cde' on the ForeignKey to tell it to look at the value of the target table's crs_cde field, rather than the (integer) primary key.

Upvotes: 3

Rustem
Rustem

Reputation: 2932

Check your column crs_cde_id - it should be Int type in database. But seems like you have chars there.

Upvotes: 0

Related Questions