[Answered ]-When i publish my Django CMS page with a ManyToManyField it's just be displayed in edit page

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'

Leave a comment