Initial commit: Product Slip Manager
This commit is contained in:
1
slips/management/__init__.py
Normal file
1
slips/management/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
# management package
|
||||
1
slips/management/commands/__init__.py
Normal file
1
slips/management/commands/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
# commands package
|
||||
43
slips/management/commands/seed_products.py
Normal file
43
slips/management/commands/seed_products.py
Normal file
@@ -0,0 +1,43 @@
|
||||
from django.core.management.base import BaseCommand
|
||||
from slips.models import Product
|
||||
|
||||
|
||||
SEED_PRODUCTS = [
|
||||
{'code': 'ICJ/408', 'description': 'DIGITAL INK RED BROWN', 'default_net_weight': 5},
|
||||
{'code': 'ICJ/407', 'description': 'DIGITAL INK RED BROWN', 'default_net_weight': 5},
|
||||
{'code': 'ICJ/309', 'description': 'DIGITAL INK CYAN', 'default_net_weight': 10},
|
||||
{'code': 'ICJ/EH630', 'description': 'DIGITAL INK BLACK', 'default_net_weight': 5},
|
||||
{'code': 'ICJ/308', 'description': 'DIGITAL INK BLUE', 'default_net_weight': 20},
|
||||
]
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = 'Seed the database with initial product data from Excel'
|
||||
|
||||
def add_arguments(self, parser):
|
||||
parser.add_argument(
|
||||
'--skip-existing',
|
||||
action='store_true',
|
||||
help='Skip products that already exist',
|
||||
)
|
||||
|
||||
def handle(self, *args, **options):
|
||||
created_count = 0
|
||||
for product_data in SEED_PRODUCTS:
|
||||
if options['skip_existing']:
|
||||
_, created = Product.objects.get_or_create(
|
||||
code=product_data['code'],
|
||||
defaults=product_data,
|
||||
)
|
||||
else:
|
||||
Product.objects.update_or_create(
|
||||
code=product_data['code'],
|
||||
defaults=product_data,
|
||||
)
|
||||
created = True
|
||||
if created:
|
||||
created_count += 1
|
||||
|
||||
self.stdout.write(
|
||||
self.style.SUCCESS(f'Seeded {created_count} products ({len(SEED_PRODUCTS)} total)')
|
||||
)
|
||||
Reference in New Issue
Block a user