I’m upgrading a project from Django 1.8 to Django 2.1.3 (yes, I know I should update more, but that’s the client requirement at this point). The exact same models.py file and exact same Django ORM query in the views.py file are in both (Python3 updates notwithstanding), the Django 1.8 version works fine but with Django 2.1.3 I’m seeing the following error:
raise FieldError('Related Field got invalid lookup: {}'.format(lookups[0]))
django.core.exceptions.FieldError: Related Field got invalid lookup: carcategory
Anybody know what gives here? The DB is the same, nothing’s been changed there, and like I said, the models.py file is exactly the same (aside from updating some ForeignKey fields with on_delete=models.CASCADE
for the Django 2.1.3 version)
Here’s the main query:
cars = CarModel.objects.only(
'car__car_id',
'car__cardate',
'car__title',
'car__location',
'car__quote',
'car__key',
'car__cartype_id',
'maker__lastname',
'maker__firstname',
'maker__middlename',
'maker__nickname',
).select_related(
'car',
'maker',
).extra(
select={
'is_position_paper': 'cartype_id = 7',
'is_null_date': 'cardate IS NULL',
'shorttitle': extra,
},
).filter(**kwargs).distinct(sort_order_for_distinct, 'car__car_id').order_by(sort_order, 'car__car_id')
Prior to update Django version, on Django 1.8 I was able to add the following filter to the above query:
kwargs['car__carcategory__category__category_id'] = categoryId
This is the CarModel table:
class CarModel(models.Model):
car_candidate_id = models.IntegerField(primary_key=True)
car = models.ForeignKey(Car, on_delete=models.PROTECT)
candidate = models.ForeignKey(Manufacturer, on_delete=models.PROTECT)
created = models.DateTimeField(blank=True, null=True)
modified = models.DateTimeField(blank=True, null=True)
class Meta:
managed = False
db_table = 'car_model'
This is the Car table:
class Car(models.Model):
car_id = models.IntegerField(primary_key=True)
cartype = models.ForeignKey('Cartype', on_delete=models.PROTECT)
title = models.CharField(max_length=200)
class Meta:
managed = False
db_table = 'car'
This is what the Carcategory table looks like:
class Carcategory(models.Model):
car_category_id = models.IntegerField(primary_key=True)
car = models.ForeignKey(Car, on_delete=models.CASCADE)
category = models.ForeignKey(Category, on_delete=models.CASCADE)
class Meta:
db_table = u'car_category'
This is the Category table:
class Category(models.Model):
category_id = models.SmallIntegerField(primary_key=True)
name = models.CharField(max_length=255)
description = models.CharField(max_length=2048, blank=True)
release_id = models.SmallIntegerField()
key = models.NullBooleanField(null=True)
rank = models.SmallIntegerField(null=True)
class Meta:
db_table = u'category'
def __unicode__(self):
return unicode(self.category_id)