I have a model MenuIngredientConsumption, which is inherited from MenuOfferOptionModificationThrough.
<code>class MenuOfferOptionModificationThrough(models.Model) :
'content of offer'
menu_offer_option_modification = models.ForeignKey(MenuOfferOptionModification, on_delete=models.CASCADE)
storage_position = models.ForeignKey(StoragePosition, on_delete=models.CASCADE)
quantity = models.DecimalField(max_digits=20, decimal_places=2, null=True)
ingredient_name = models.CharField(
max_length=200,
validators=[MinLengthValidator(2, "Title must be greater than 2 characters")]
)
class MenuIngredientConsumption(MenuOfferOptionModificationThrough) :
created_at = models.DateTimeField(auto_now_add=True)
</code>
<code>class MenuOfferOptionModificationThrough(models.Model) :
'content of offer'
menu_offer_option_modification = models.ForeignKey(MenuOfferOptionModification, on_delete=models.CASCADE)
storage_position = models.ForeignKey(StoragePosition, on_delete=models.CASCADE)
quantity = models.DecimalField(max_digits=20, decimal_places=2, null=True)
ingredient_name = models.CharField(
max_length=200,
validators=[MinLengthValidator(2, "Title must be greater than 2 characters")]
)
class MenuIngredientConsumption(MenuOfferOptionModificationThrough) :
created_at = models.DateTimeField(auto_now_add=True)
</code>
class MenuOfferOptionModificationThrough(models.Model) :
'content of offer'
menu_offer_option_modification = models.ForeignKey(MenuOfferOptionModification, on_delete=models.CASCADE)
storage_position = models.ForeignKey(StoragePosition, on_delete=models.CASCADE)
quantity = models.DecimalField(max_digits=20, decimal_places=2, null=True)
ingredient_name = models.CharField(
max_length=200,
validators=[MinLengthValidator(2, "Title must be greater than 2 characters")]
)
class MenuIngredientConsumption(MenuOfferOptionModificationThrough) :
created_at = models.DateTimeField(auto_now_add=True)
I have 2 created objects of MenuOfferOptionModificationThrough.
<code>MenuOfferOptionModificationThrough(..., ingredient_name="potato")
MenuOfferOptionModificationThrough(..., ingredient_name="tomato")
</code>
<code>MenuOfferOptionModificationThrough(..., ingredient_name="potato")
MenuOfferOptionModificationThrough(..., ingredient_name="tomato")
</code>
MenuOfferOptionModificationThrough(..., ingredient_name="potato")
MenuOfferOptionModificationThrough(..., ingredient_name="tomato")
So i want to copy these 2 objects of MenuOfferOptionModificationThrough to MenuIngredientConsumption. And i wrote a code in view.py:
<code> def post(self, request, **kwargs):
gastronomy = get_object_or_404(Gastronomy, id=self.kwargs['gastronomy_pk'])
order_modification_through = get_object_or_404(OrderModificationThrough, id=self.kwargs['order_modification_pk'])
menu_offer_option_modification = order_modification_through.menu_offer_option_modification
quantity = order_modification_through.quantity
menu_offer_option_modification_through_list = MenuOfferOptionModificationThrough.objects.filter(menu_offer_option_modification=menu_offer_option_modification)
if menu_offer_option_modification_through_list:
for i in range(quantity):
for ingredient in menu_offer_option_modification_through_list:
menu_ingredient_consumption = MenuIngredientConsumption(
menu_offer_option_modification = ingredient.menu_offer_option_modification,
storage_position = ingredient.storage_position,
quantity = ingredient.quantity,
ingredient_name = ingredient.ingredient_name,
)
menu_ingredient_consumption.save()
</code>
<code> def post(self, request, **kwargs):
gastronomy = get_object_or_404(Gastronomy, id=self.kwargs['gastronomy_pk'])
order_modification_through = get_object_or_404(OrderModificationThrough, id=self.kwargs['order_modification_pk'])
menu_offer_option_modification = order_modification_through.menu_offer_option_modification
quantity = order_modification_through.quantity
menu_offer_option_modification_through_list = MenuOfferOptionModificationThrough.objects.filter(menu_offer_option_modification=menu_offer_option_modification)
if menu_offer_option_modification_through_list:
for i in range(quantity):
for ingredient in menu_offer_option_modification_through_list:
menu_ingredient_consumption = MenuIngredientConsumption(
menu_offer_option_modification = ingredient.menu_offer_option_modification,
storage_position = ingredient.storage_position,
quantity = ingredient.quantity,
ingredient_name = ingredient.ingredient_name,
)
menu_ingredient_consumption.save()
</code>
def post(self, request, **kwargs):
gastronomy = get_object_or_404(Gastronomy, id=self.kwargs['gastronomy_pk'])
order_modification_through = get_object_or_404(OrderModificationThrough, id=self.kwargs['order_modification_pk'])
menu_offer_option_modification = order_modification_through.menu_offer_option_modification
quantity = order_modification_through.quantity
menu_offer_option_modification_through_list = MenuOfferOptionModificationThrough.objects.filter(menu_offer_option_modification=menu_offer_option_modification)
if menu_offer_option_modification_through_list:
for i in range(quantity):
for ingredient in menu_offer_option_modification_through_list:
menu_ingredient_consumption = MenuIngredientConsumption(
menu_offer_option_modification = ingredient.menu_offer_option_modification,
storage_position = ingredient.storage_position,
quantity = ingredient.quantity,
ingredient_name = ingredient.ingredient_name,
)
menu_ingredient_consumption.save()
Yes, I copy 2 objects to MenuIngredientConsumption, but also i have 6 objects of MenuOfferOptionModificationThrough. But there should be 2 still (I didn;t work with it). I don’t understand why it happens. Please, help.