How to implement multi-step product creation with related models (Tax, Currency, Delivery Area, Category) in Django Rest Framework?

I am working on a product creation process using Django Rest Framework (DRF), and I have models for Tax, Currency, DeliveryArea, Category, and Product. The creation process is divided into multiple steps, with each step capturing different parts of the product's information. Here's what I am trying to achieve:

Models:

  • Product: The main model that represents the product.
  • Category: The category to which the product belongs.
  • Currency: The currency for the product's price.
  • DeliveryArea: The delivery area for the product.
  • Tax: The tax details for the product.

Steps in the Process:

  1. Step 1: Product name, description, and category.
  2. Step 2: Price, currency, and delivery area.
  3. Step 3: Tax information.

What I Need:

  • Data Persistence: I need to save the data progressively as the user completes each step of the form.
  • Step-by-step editing: Users should be able to return to any previous step and update their inputs without losing the progress in the steps they've already completed.
  • Related Models: I need to handle the relationships between the Product model and the other models (Category, Currency, DeliveryArea, and Tax).

Approach I've Considered:

  • I plan to use DRF serializers for each step, with the relevant fields for that step.
  • I want to use a custom viewset with actions for each step of the form.
  • I would like to handle the saving of each step's data in the database progressively and ensure that each step's data is stored in the corresponding models.

Example Models:

class Category(models.Model):
    name = models.CharField(max_length=255)

class Currency(models.Model):
    code = models.CharField(max_length=3)

class DeliveryArea(models.Model):
    area_name = models.CharField(max_length=255)

class Tax(models.Model):
    name = models.CharField(max_length=255)
    percentage = models.DecimalField(max_digits=5, decimal_places=2)

class Product(models.Model):
    name = models.CharField(max_length=255)
    description = models.TextField()
    category = models.ForeignKey(Category, on_delete=models.SET_NULL, null=True, blank=True)
    price = models.DecimalField(max_digits=10, decimal_places=2, null=True, blank=True)
    currency = models.ForeignKey(Currency, on_delete=models.SET_NULL, null=True, blank=True)
    delivery_area = models.ForeignKey(DeliveryArea, on_delete=models.SET_NULL, null=True, blank=True)
    tax = models.ForeignKey(Tax, on_delete=models.SET_NULL, null=True, blank=True)

I tried makeing a model for each step and make each step point to the previous one like reversed linked lists. example: step 1 <- step 2 <- step 3 but I think this implementation is unpractical, someone has suggested me to use viewsets (I'm still a drf rookie). keep in mind that the code I provided is just example. I can't share company code but some fields in the product models can't be null nor blank however they're only set by the user in like a later step.

Вернуться на верх