When i publish my Django CMS page with a ManyToManyField it’s just be displayed in edit page.
This is my model code:
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 __str__(self):
return "{}{}".format(self.title, self.subtitle)
class Meta:
verbose_name = 'Carrossel de Produtos'
verbose_name_plural = 'Carrossíes de Produtos'
This is my CSM plugin code:
@plugin_pool.register_plugin
class ProductCarroselPlugin5(CMSPluginBase):
module = 'Produto'
name = '04 - Produtos - Carrossel de Produtos'
model = ProductCarrossel
render_template = "productapp/product_carrossel.html"
allow_children = False
fieldsets = [
('Bloco Principal', {
'fields': (
('title'),
('subtitle'),
# ('text_color'),
('show_only'),
('categories'),
('subcategories'),
('attributes'),
('attribute_options'),
('products'),
)
}),
]
def render(self, context, instance, placeholder):
if instance.show_only == 'category':
carrossel_products = Product.objects.filter(
category__in=instance.categories.all(),
productimage__isnull=False, # Apenas produtos que tenham imagem
productprice__price__gt=0, # Apenas produtos com preço > 0
disp='disponivel'
).order_by('?')[:12]
elif instance.show_only == 'subcategory':
carrossel_products = Product.objects.filter(
subcategory__id__in=instance.subcategories.all(),
productimage__isnull=False, # Apenas produtos que tenham imagem
productprice__price__gt=0, # Apenas produtos com preço > 0
disp='disponivel'
).order_by('?')[:12]
elif instance.show_only == 'attribute':
carrossel_products = Product.objects.filter(
attribute_options__attribute__in=instance.attributes.all(),
productimage__isnull=False, # Apenas produtos que tenham imagem
productprice__price__gt=0, # Apenas produtos com preço > 0
disp='disponivel'
).order_by('?')[:12]
elif instance.show_only == 'attribute_option':
carrossel_products = Product.objects.filter(
attribute_options__id__in=instance.attribute_options.all(),
productimage__isnull=False, # Apenas produtos que tenham imagem
productprice__price__gt=0, # Apenas produtos com preço > 0
disp='disponivel'
).order_by('?')[:12]
elif instance.show_only == 'product':
carrossel_products = instance.products.filter(
productimage__isnull=False, # Apenas produtos que tenham imagem
productprice__price__gt=0, # Apenas produtos com preço > 0
disp='disponivel'
).order_by('?')[:12]
for product in carrossel_products:
#Pegando preço dos produtos
product_price = ProductPrice.objects.filter(id_product=product).get()
product.price = product_price.price
context.update({
'instance': instance,
'products': carrossel_products,
})
return context
This is the plugin in edit page and published page:
How to solve this problem?
I just tried to clean the cache and review my code. I tried to edit the page removing and adding the plugin again but not worked too. I could think that it’s because i user the “related_name” but it’s not too.
Thanks for the help. You are amazing!