Queryset: Getting the description of the accessories on request (Django)

Asked

Viewed 225 times

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 on request?

I mean, in my template I need to:

{{ object.kit_optional.accessory }} e
{{ object.kit_optional.price_accessory }}
{{ object.kit_optional.quantity_accessory }}
  • What would that be object? You’re a little confused, there’s a class Kit and the class KitDetail, it would not be better to unite these two?

  • @Orion O KitDetail exists because each kit can various accessories. You think it would be better to use a M2m between Kit and Accessory?

  • It would be better if.

1 answer

1


I would configure the model 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 countless items.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.