I am working on a code base where the original developer created a ProductType model
with several rows in the database representing a few product types. Throughout the code, there are statements like ProductType.objects.get(slug='foo_product_type')
. These statements will cause a hard crash if fixtures haven't loaded the ProductType
record with slug = 'foo_product_type'
. What is the best practice to define the ProductType
with slug 'foo_product_type'
? It seems like python code shouldn't depend on fixtures being loaded, but I really can't think of another solution. It feels like a bad practice for your server logic to depend on certain database fields, but im not sure I can think of another method.
I have worked with using choices=
on the model definition, but that seems to be more for data integrity as opposed to loading a record with details like price
so that doesn't help me. I currently load fixtures after migrating a new database, and that works, but I'm curious if anyone has a better solution.
if ProductType.objects.get(slug='foo_product_type'):
when ProductType
with slug 'foo_product_type'
doesn't exist.
When the fixture is loaded, the object is grabbed for use in code. When record is missing, app crashes.