From 3c5169c79310b010c0c6b3c30e57203a4e443385 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Thu, 4 Feb 2021 23:10:10 +1100 Subject: [PATCH] So I learned something today... In migration files you can access the "historical" pythonic model, and use that, with *all* the helpers, rather than writing clunky old SQL!!!! :'( --- .../migrations/0019_auto_20200413_0642.py | 23 ++++++++----------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/InvenTree/company/migrations/0019_auto_20200413_0642.py b/InvenTree/company/migrations/0019_auto_20200413_0642.py index 2468b864c2..af84b485b3 100644 --- a/InvenTree/company/migrations/0019_auto_20200413_0642.py +++ b/InvenTree/company/migrations/0019_auto_20200413_0642.py @@ -165,24 +165,19 @@ def associate_manufacturers(apps, schema_editor): def create_manufacturer(part_id, input_name, company_name): """ Create a new manufacturer """ - # Manually create a new database row - # Note: Have to fill out all empty string values! - new_manufacturer_query = f"insert into company_company (name, description, is_customer, is_supplier, is_manufacturer, address, website, phone, email, contact, link, notes) values ('{company_name}', '{company_name}', 0, 0, 1, '', '', '', '', '', '', '');" + Company = apps.get_model('company', 'company') - cursor = connection.cursor() - - cursor.execute(new_manufacturer_query) - - # Extract the company back from the database - response = cursor.execute(f"select id from company_company where name='{company_name}';") - row = cursor.fetchone() - manufacturer_id = int(row[0]) + manufacturer = Company.objects.create( + name=company_name, + description=company_name, + is_manufacturer=True + ) # Map both names to the same company - links[input_name] = manufacturer_id - links[company_name] = manufacturer_id + links[input_name] = manufacturer.pk + links[company_name] = manufacturer.pk - companies[company_name] = manufacturer_id + companies[company_name] = manufacturer.pk print(" - Part[{pk}]: Created new manufacturer: '{name}'".format(pk=part_id, name=company_name))