0
I’m making a system in which I need to show data that is registered in another Model, which in this case is the Category field that is in the Model Items and when showing it, the user would select one of them and the system would show the data that are only linked to the selected category.
Ex.:
Locker register
Name:
Description:
Categories: Footwear| Pants| Blouses| * I selected Blouses
Items: Here the system shows only items in the Blouses category with the option to select the item
Locker: Will contain the selected items.
models.py items
from django.db import models
class Categoria(models.Model):
name_categoria = models.CharField(max_length=100, null=True)
def __str__(self):
return self.name_categoria
class Cor(models.Model):
cor_item = models.CharField(max_length=50, null=True)
def __str__(self):
return self.cor_item
class ItenManager(models.Manager):
def search(self, query):
return self.get_queryset().filter(
models.Q(name__icontains=query) | \
models.Q(cor__icontains=query)
)
class Itens(models.Model):
name = models.CharField('Nome:', max_length=100)
slug = models.SlugField('Atalho:')
categoria = models.ForeignKey('Categoria',on_delete=models.CASCADE)
cor = models.ForeignKey('Cor', on_delete=models.CASCADE)
image = models.ImageField(
upload_to='itens_imagens', verbose_name='Imagem:', null=True, blank=True
)
objects = ItenManager()
def __str__(self):
return self.name
@models.permalink
def get_absolute_url(self):
return ('itens:details', (), {'slug': self.slug})
class Meta:
verbose_name = 'Item'
verbose_name_plural = 'Itens'
ordering = ['name']
cabinet models.py
from django.db import models
class Armario(models.Model):
name= models.CharField('Nome',max_length=50, blank=False)
slug = models.SlugField('Atalho')
descricao = models.CharField('Descrição',max_length=100, blank=True)
created_at = models.DateField('Criado em', auto_now_add=True)
updated_at = models.DateField('Atualizado em',auto_now=True)
def __str__(self):
return self.name
class Meta:
verbose_name = 'Armário'
verbose_name_plural = 'Armários'
ordering = ['name']
You can use javascript (ajax) to send a request to the Django url when the user selects the category.
– Camilo Santos