1đ
I needed to add the function âcopy_relationsâ inside model. The final model code was like:
class ProductCarrossel(CMSPlugin):
title = models.CharField(
verbose_name='TĂtulo do Carrossel',
max_length=150,
null=True,
blank=True,
)
subtitle = HTMLField(
verbose_name='SubtĂtulo do Carrossel',
max_length=1000,
help_text='Insira um subtĂtulo para o Carrossel',
null=True,
blank=True,
default='',
)
show_only = models.CharField(
max_length=16,
choices=ProductCarrosselTypes.choices,
default=ProductCarrosselTypes.CATEGORY,
verbose_name='Usar somente a opção',
help_text='Selecione entre Categoria, Subcategoria, Atributo ou Opção de Atributo',
)
categories = models.ManyToManyField(
Category,
related_name="products_to_carrossel_by_category",
blank=True,
verbose_name='Selecione as Categorias desejadas',
)
subcategories = models.ManyToManyField(
Subcategory,
related_name="products_to_carrossel_by_subcategory",
blank=True,
verbose_name='Selecione as Subategorias desejadas',
)
attributes = models.ManyToManyField(
Attribute,
related_name="products_to_carrossel_by_attribute",
blank=True,
verbose_name='Selecione os Atributos desejadas',
)
attribute_options = models.ManyToManyField(
AttributeOption,
related_name="products_to_carrossel_by_attribute_option",
blank=True,
verbose_name='Selecione as OpçÔes de Atributos desejadas',
)
products = models.ManyToManyField(
Product,
related_name="products_to_carrossel_by_product",
blank=True,
)
text_color = models.CharField(
max_length=30,
choices=ColorClassesToTexts.choices,
default=ColorClassesToTexts.AZUL,
verbose_name='Somente os textos da capa',
help_text='Selecione a cor dos textos',
)
def copy_relations(self, oldinstance):
self.categories.set(oldinstance.categories.all())
self.subcategories.set(oldinstance.subcategories.all())
self.attributes.set(oldinstance.attributes.all())
self.attribute_options.set(oldinstance.attribute_options.all())
self.products.set(oldinstance.products.all())
def __str__(self):
return "{}{}".format(self.title, self.subtitle)
class Meta:
verbose_name = 'Carrossel de Produtos'
verbose_name_plural = 'CarrossĂes de Produtos'
đ€Daniel Oliveira
Source:stackexchange.com