feat(demo_data): enhance data generation with get_or_create

- Replaced `create` operations with `get_or_create` to ensure idempotency during data generation.
- Avoided redundant user, product image, and post creation when duplicates exist.
- Updated user and stock handling to leverage defaults for improved clarity.
- Prevented overwriting existing blog post and product image content.
This commit is contained in:
Egor Pavlovich Gorbunov 2026-02-27 22:27:39 +03:00
parent df0d503c13
commit 09610d98a2

View file

@ -257,8 +257,8 @@ class Command(BaseCommand):
Attribute.objects.filter(group__name__in=group_names).delete() Attribute.objects.filter(group__name__in=group_names).delete()
AttributeGroup.objects.filter(name__in=group_names).delete() AttributeGroup.objects.filter(name__in=group_names).delete()
self.staff_user.delete() User.objects.filter(email=f"staff@{DEMO_EMAIL_DOMAIN}").delete()
self.super_user.delete() User.objects.filter(email=f"super@{DEMO_EMAIL_DOMAIN}").delete()
self.stdout.write("") self.stdout.write("")
self.stdout.write(self.style.SUCCESS("=" * 50)) self.stdout.write(self.style.SUCCESS("=" * 50))
@ -409,13 +409,15 @@ class Command(BaseCommand):
product.description_ru_ru = prod_data["description_ru"] # ty: ignore[invalid-assignment] product.description_ru_ru = prod_data["description_ru"] # ty: ignore[invalid-assignment]
product.save() product.save()
Stock.objects.create( Stock.objects.get_or_create(
vendor=vendor, vendor=vendor,
product=product, product=product,
sku=f"GS-{prod_data['partnumber']}", defaults={
price=prod_data["price"], "sku": f"GS-{prod_data['partnumber']}",
purchase_price=prod_data["purchase_price"], "price": prod_data["price"],
quantity=prod_data["quantity"], "purchase_price": prod_data["purchase_price"],
"quantity": prod_data["quantity"],
},
) )
# Add product image # Add product image
@ -475,6 +477,9 @@ class Command(BaseCommand):
def _save_product_image( def _save_product_image(
self, product: Product, image_path: Path, priority: int self, product: Product, image_path: Path, priority: int
) -> None: ) -> None:
if product.images.filter(priority=priority).exists():
return
with open(image_path, "rb") as f: with open(image_path, "rb") as f:
image_content = f.read() image_content = f.read()
@ -513,14 +518,16 @@ class Command(BaseCommand):
existing_emails.add(email) existing_emails.add(email)
# Create user user, created = User.objects.get_or_create(
user = User(
email=email, email=email,
first_name=first_name, defaults={
last_name=last_name, "first_name": first_name,
is_active=True, "last_name": last_name,
is_verified=True, "is_active": True,
"is_verified": True,
},
) )
if created:
user.set_password(password) user.set_password(password)
user.save() user.save()
@ -594,12 +601,14 @@ class Command(BaseCommand):
address = Address.objects.filter(user=user).first() address = Address.objects.filter(user=user).first()
order = Order.objects.create( order, _ = Order.objects.get_or_create(
user=user, user=user,
status=status, status=status,
buy_time=order_date, buy_time=order_date,
billing_address=address, defaults={
shipping_address=address, "billing_address": address,
"shipping_address": address,
},
) )
Order.objects.filter(pk=order.pk).update(created=order_date) Order.objects.filter(pk=order.pk).update(created=order_date)
@ -620,12 +629,14 @@ class Command(BaseCommand):
else: else:
op_status = random.choice(["ACCEPTED", "PENDING"]) op_status = random.choice(["ACCEPTED", "PENDING"])
OrderProduct.objects.create( OrderProduct.objects.get_or_create(
order=order, order=order,
product=product, product=product,
quantity=quantity, defaults={
buy_price=round(price, 2), "quantity": quantity,
status=op_status, "buy_price": round(price, 2),
"status": op_status,
},
) )
orders.append(order) orders.append(order)
@ -682,9 +693,6 @@ class Command(BaseCommand):
tag.save() tag.save()
for post_data in data.get("blog_posts", []): for post_data in data.get("blog_posts", []):
if Post.objects.filter(title=post_data["title"]).exists():
continue
content_en = self._load_blog_content(post_data["content_file"], "en") content_en = self._load_blog_content(post_data["content_file"], "en")
content_ru = self._load_blog_content(post_data["content_file"], "ru") content_ru = self._load_blog_content(post_data["content_file"], "ru")
@ -696,13 +704,16 @@ class Command(BaseCommand):
) )
continue continue
post = Post( post, created = Post.objects.get_or_create(
author=author,
title=post_data["title"], title=post_data["title"],
content=content_en, defaults={
meta_description=post_data.get("meta_description", ""), "author": author,
is_static_page=post_data.get("is_static_page", False), "content": content_en,
"meta_description": post_data.get("meta_description", ""),
"is_static_page": post_data.get("is_static_page", False),
},
) )
if created:
if "title_ru" in post_data: if "title_ru" in post_data:
post.title_ru_ru = post_data["title_ru"] # ty:ignore[unresolved-attribute] post.title_ru_ru = post_data["title_ru"] # ty:ignore[unresolved-attribute]
if content_ru: if content_ru: