Queryset: Getting the description of the accessories in the application (Django)

2

Consider the example below:

class Vehicle(models.Model):
    vehicle = models.CharField(_('veículo'), max_length=50, unique=True)
    price = models.DecimalField(_(u'preço'), max_digits=8, decimal_places=2)
    kit_fabric = models.ForeignKey(
        "Kit", verbose_name=u'kit de fábrica', related_name='vehicle_kit')


class Accessory(models.Model):
    accessory = models.CharField(_(u'accessório'), max_length=50)
    price_accessory = models.DecimalField(
        _(u'preço'), max_digits=8, decimal_places=2)


class Kit(models.Model):
    kit = models.CharField(max_length=50)


class KitDetail(models.Model):
    kit = models.ForeignKey("Kit", verbose_name='kit', related_name='kit_det')
    accessory = models.ForeignKey(
        "Accessory", verbose_name=u'accessório', related_name='accessory_kit')
    quantity_accessory = models.PositiveIntegerField(_('quantidade'))

    def __str__(self):
        return str(self.kit)


class Ordered(TimeStampedModel):
    customer = models.ForeignKey("Customer", verbose_name='cliente')
    employee = models.ForeignKey("Employee", verbose_name=u'funcionário')
    vehicle = models.ForeignKey("Vehicle", verbose_name=u'veículo')
    kit_optional = models.ForeignKey("Kit", verbose_name='kit opcional')
    dealership = models.ForeignKey(
        "Dealership", verbose_name=u'concessionária')
    kiosk = models.ForeignKey("Kiosk", verbose_name='quiosque')
    status = models.CharField(max_length=2, choices=status_list, default='p')

How do I get the description of the accessories in the order?

That is, in my template I need:

{{ object.kit_optional.accessory }} e
{{ object.kit_optional.price_accessory }}
{{ object.kit_optional.quantity_accessory }}
    
asked by anonymous 07.06.2015 / 08:48

1 answer

1

I would configure the template as follows (example):

model

class Veiculo(models.Model):
    nome = models.CharField(_('nome'), max_length=50, unique=True)
    valor = models.DecimalField(_(u'preço'), max_digits=8, decimal_places=2)
    kit = models.ForeignKey("Kit", verbose_name=u'kit de fábrica')

class Kit(models.Model):
    kit = models.CharField(max_length=50)
    acessorios = models.ManyToManyField('Acessorio', blank=True, null=True)

class Acessorio(models.Model):
    nome = models.CharField(_(u'accessório'), max_length=50)
    valor = models.DecimalField(_(u'preço'), max_digits=8, decimal_places=2)
    quantidade = models.PositiveIntegerField()

class Pedido(TimeStampedModel):
    customer = models.ForeignKey("Customer", verbose_name='cliente')
    employee = models.ForeignKey("Employee", verbose_name=u'funcionário')
    veiculo = models.ForeignKey("Veiculo", verbose_name=u'veículo')
    kit_optional = models.ForeignKey("Kit", verbose_name='kit opcional')
    dealership = models.ForeignKey(
        "Dealership", verbose_name=u'concessionária')
    kiosk = models.ForeignKey("Kiosk", verbose_name='quiosque')
    status = models.CharField(max_length=2, choices=status_list, default='p')

view

pedido = Pedido.objects.all()[0]

template

{% for acessorio in pedido.kit_optional.acessorios.all %}
    {{ acessorio.nome }} {{ acessorio.valor }} {{ acessorio.quantidade }}
{% endfor %}

This way a kit can have many items.

    
07.06.2015 / 22:07