Relationship Django Models

0

I have a model named Item and I made a call Compra . I have been able to relate several items to each purchase, but I do not know how to relate the quantity of each item to each purchase.

class Item(models.Model):
nome = models.CharField('Nome', max_length=100, null=False, unique=True)
estoque = models.PositiveIntegerField('Estoque', blank=False, default=0)
estoque_min = models.PositiveIntegerField('Estoque Min', blank=False, default=0)
slug = models.SlugField('Atalho')

def __str__(self):
    return self.nome

Model Buy:

class Compra(models.Model):
fornecedor = models.ForeignKey(Fornecedor, verbose_name='Fornecedor', on_delete=models.CASCADE)
item = models.ManyToManyField(Item, verbose_name='Item', related_name='Itens')
data_compra = models.DateTimeField('Data de Compra', auto_now=True)
data_entrega = models.DateTimeField('Data de Entrega')
entregue = models.BooleanField('Entregue', default=False)

def get_absolute_url(self):
    return ('compras/' + str(self.pk))

I made a many-to-many relationship with the items, in Django Admin it looks like this:

I wanted a suggestion of what logic to use to put quantity in each selected item, this amount being related to each purchase, not to the stock of the item.

EDIT: I used through and worked, but the problem is different.

def compras_detalhe(request, pk):
compras = Compra.objects.all().filter(id=pk)

itens = []
for compra in compras:
    for item in compra.itens.all():
        print(item)
        itens.append(item)

With this code I can display the items that were purchased, but I can not access the quantity field, which is in the ItemCompra (intermediate in the through) model.

class Compra(models.Model):
fornecedor = models.ForeignKey(Fornecedor, verbose_name='Fornecedor', on_delete=models.CASCADE)
itens = models.ManyToManyField(Item, through='ItemCompra')
cliente = models.ForeignKey(Cliente, verbose_name='Cliente', on_delete=models.CASCADE)
data_compra = models.DateTimeField('Data de Compra', auto_now=True)
data_entrega = models.DateTimeField('Data de Entrega')
nota_fiscal = models.CharField('Nota Fiscal', max_length=100, default='-')
entregue = models.BooleanField('Entregue', default=False)

def get_absolute_url(self):
    return ('compras/' + str(self.pk))

class ItemCompra(models.Model):
item = models.ForeignKey(Item, verbose_name='Item', related_name='Itens', on_delete=models.CASCADE)
quantidade = models.IntegerField('Quantidade', default=0)
compra_id = models.ForeignKey(Compra, on_delete=models.CASCADE, related_name='Compra', null=True)

def __str__(self):
    return (self.item.nome + str(self.quantidade))

class Item(models.Model):
    nome = models.CharField('Nome', max_length=100, null=False, unique=True)
    unidade = models.ForeignKey(Unidade, verbose_name='Unidade', on_delete=models.CASCADE)
    estoque = models.PositiveIntegerField('Estoque', blank=False, default=0)
    estoque_min = models.PositiveIntegerField('Estoque Min', blank=False, default=0)
    slug = models.SlugField('Atalho')

    def __str__(self):
        return self.nome
    
asked by anonymous 09.11.2018 / 13:27

1 answer

0

Use the through parameter of ManyToManyField to define a model that will serve as the connection between the other two. In this model you can add additional data:

class Item(models.Model):
    nome = models.CharField('Nome', max_length=100, null=False, unique=True)

class Compra(models.Model):
    item = models.ManyToManyField(Item, verbose_name='Item', 
        related_name='Itens', through='ItemCompra')
    data_compra = models.DateTimeField('Data de Compra', auto_now=True)

class ItemCompra(models.Model):
    item = models.ForeignKey(Item)
    compra = models.ForeignKey(Compra)
    qtd = models.PositiveIntegerField()
    
09.11.2018 / 16:29