I'm trying to update some database records via a Django data migration, using the following migration file:
from django.apps import apps
from django.db import IntegrityError, migrations
def exclude_pending_drivers(apps, schema_edition):
Driver = apps.get_model("team", "Driver")
pending_drivers = Driver.objects.filter(type=Driver.PENDING)
for driver in pending_drivers:
driver.show_in_app = False
Driver.objects.bulk_update(pending_drivers, ['show_in_app'])
class Migration(migrations.Migration):
dependencies = [
('team', '0002_add_show_in_app_to_driver'),
]
operations = [
migrations.RunPython(exclude_pending_drivers),
]
I'm getting an error when I run it:
AttributeError: type object 'Driver' has no attribute 'PENDING'
PENDING is defined as a class variable in the model:
class Driver(models.Model):
PENDING = 1
CONFIRMED = 2
show_in_app = models.BooleanField(default=True)
# etc
I can run the exact migration code above in a manage.py shell without errors, and it also runs fine if I use from team.models import Driver instead of apps.get_model("team", "Driver") (although obviously that's not advised).
What gives? It looks like the Driver class, when it's included via apps.get_model(), is different to a straight import, but surely that's not the case??