Y
Y
Yarovoy Ivan2015-01-22 17:04:55
Django
Yarovoy Ivan, 2015-01-22 17:04:55

Why takes ManyToManyField on save?

Hello everyone , the
problem is that when
from django.db import models
from apps.product.models import Product
from apps.core.models import Category
class CategoryConverter(models.Model):
product = models.ManyToManyField(Product, blank=True, null =True)
category = models.ForeignKey(Category, blank=True, null=True)
def save(self, *args, **kwargs):
print(self.category)
print(self.product)
if self.product and self .category:
for product in self.product.all():
product.category.add(self.category)
product.save()
return super(CategoryConverter, self).save(*args, **kwargs)
I select products in the admin but when saving self.product is empty Thank you in
advance )

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rostislav Grigoriev, 2015-01-24
@crazyzubr

  • need to post the code in the question
  • why is save overridden for this purpose, why is ModelForm not used?
  • the manytomany field is filled only for existing objects, that is, you must first save the object, and then add categories to it

In addition, there are obvious errors in the code. The loop adds objects to the ForeignKey field, etc.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question