How can I split orders by attributes in my API

I have a method that creates orders from the user’s cart. For the courier to take an order from different restaurants, the order is divided into several. But at the moment I’m splitting the order just by the dish in the cart. How to make an order split by restaurants? that is, if a user orders 5 dishes from two different restaurants, then 2 orders were formed.

views.py

@action(methods=['PUT'], detail=False, url_path='current_customer_cart/add_to_order')
    def add_cart_to_order(self, *args, **kwargs):
        cart = Cart.objects.get(owner=self.request.user.customer)
        cart_meals = CartMeal.objects.filter(cart=cart)
        data = self.request.data

        for cart_meal in cart_meals:
            order = Order.objects.create(customer=self.request.user.customer,
                                         cart_meal=cart_meal,
                                         first_name=data['first_name'],
                                         last_name=data['last_name'],
                                         phone=data['phone'],
                                         address=data.get('address', self.request.user.customer.home_address),
                                         restaurant_address=cart_meal.meal.restaurant.address,
                                         )
            order.save()
        return response.Response({"detail": "Order is created", "added": True})

models.py

class Order(models.Model):
    """User's order"""

    customer = models.ForeignKey(Customer, on_delete=models.CASCADE, related_name='related_orders')
    first_name = models.CharField(max_length=255)
    last_name = models.CharField(max_length=255)
    phone = models.CharField(max_length=20)
    cart_meal = models.ForeignKey(CartMeal, on_delete=models.CASCADE, null=True, blank=True)
    restaurant_address = models.CharField(max_length=1024, null=True)
    address = models.CharField(max_length=1024)
    status = models.CharField(max_length=100, choices=STATUS_CHOICES, default=STATUS_NEW)
    created_at = models.DateTimeField(auto_now=True)
    delivered_at = models.DateTimeField(null=True, blank=True)
    courier = models.OneToOneField('Courier', on_delete=models.SET_NULL, null=True, blank=True)
class CartMeal(models.Model):
    """Cart Meal"""

    user = models.ForeignKey('Customer', on_delete=models.CASCADE)
    cart = models.ForeignKey('Cart', verbose_name='Cart', on_delete=models.CASCADE, related_name='related_meals')
    meal = models.ForeignKey(Meal, verbose_name='Meal', on_delete=models.CASCADE)
    qty = models.IntegerField(default=1)
    final_price = models.DecimalField(max_digits=9, decimal_places=2)
class Meal(models.Model):
    """Meal"""

    title = models.CharField(max_length=255)
    description = models.TextField(default='The description will be later')
    price = models.DecimalField(max_digits=9, decimal_places=2)
    discount = models.IntegerField(default=0)
    restaurant = models.ForeignKey(Restaurant, on_delete=models.CASCADE, null=True)
    slug = models.SlugField(unique=True)
class Restaurant(models.Model):
    """Restaurant"""

    name = models.CharField(max_length=255)
    slug = models.SlugField(unique=True)
    address = models.CharField(max_length=1024)
    owner = models.ForeignKey('Restaurateur', on_delete=models.CASCADE, null=True)
    meals = models.ManyToManyField('Meal', related_name='related_restaurant', blank=True)

How can I do this, please help me

Answer

You can group your meals with respect to resturants.

import itertools
from core.models import CartMeal, Order


for restaurant, cart_meals in itertools.groupby(CartMeal.objects.order_by('meal__restaurant'), lambda s: s.meal.restaurant):
    order = Order.objects.create(
        customer=self.request.user.customer,
        first_name=data['first_name'],
        last_name=data['last_name'],
        phone=data['phone'],
        address=data.get('address', self.request.user.customer.home_address),
        restaurant_address=cart_meal.meal.restaurant.address,
    )
    order.cart_meal.set([cart_meal for cart_meal in cart_meals])

Ref: The answer is formulated by taking help from following answer.

https://stackoverflow.com/a/57897654/14005534