Valueerror: Cannot assign "<class 'calendario.models.Shopping'>": "Parceling.shopping" must be a "Shopping" instance

Asked

Viewed 40 times

1

I am trying to create a table of installments, in which the user will inform the purchase value, input (if you have), the amount of installments and the date of purchase. With this data I have to take the date of purchase and show the dates that the user should pay.

First Problem - I’m not able to put in the BD the installment because it generates the error Valueerror: Cannot assign "<class 'calendario.models.Compras'>": "Parcelamento.compras" must be a "Compras" instance..

Second problem - I have no idea how to put the date list in the comic book.

my models.py:

class Compras(models.Model):
    nome_compra = models.CharField(max_length=200)
    valor_compra = models.FloatField()
    entrada = models.FloatField()
    data_compra = models.DateField()
    tipo_parcelamento = models.IntegerField()
    quantidade_parcelas = models.IntegerField()
    data_criacao = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.nome_compra


class Parcelamento(models.Model):
    compras = models.ForeignKey(Compras, on_delete=models.CASCADE)
    valor_parcela = models.FloatField()
    data_parcelas = models.DateField()

In my utils.py

def gravar_dados(**kwars):
    nome_compra, valor_compra, entrada, data_compra, tipo_parcelamento, quantidade_parcelas = kwars.values()
    Compras.objects.create(nome_compra=nome_compra,
                           valor_compra=valor_compra,
                           entrada=entrada,
                           data_compra=data_compra,
                           tipo_parcelamento=tipo_parcelamento,
                           quantidade_parcelas=quantidade_parcelas,
                           )

    valor_parcela = (valor_compra - entrada) / quantidade_parcelas
    Parcelamento.objects.create(compras=Compras,
                                valor_compra=valor_parcela,
                                data_parcelas=,
                                )

where on that parcel date, was to pick up the dates I have to calculate.

1 answer

0


As for your first question, in your function of recording the data you do not store in a variable the object of the purchase you created, so in the line that is creating the Installment you are sending your model Class "Purchases". In order to achieve the relationship between the two classes your code should present:

nova_compra = Compras.objects.create(nome_compra=nome_compra,...)
Parcelamento.objects.create(compras=nova_compra,
                                valor_compra=valor_parcela,
                                data_parcelas=,
                                )

Django returns when using objects.create the object you just created, including your key (id) that you then pass to the installment, through this variable nova_compra.

That said, I think there’s a better way to think about this code, without needing a Installment model, without saving the installments in your database.

I think you can create a method within the Purchases class that already does this calculation and returns the installments, follows an example of how to do this if the installments are equal and have to be paid monthly (I don’t know what the variable type is):

import datetime
from dateutil import relativedelta

class Compras(models.Model):
    nome_compra = models.CharField(max_length=200)
    valor_compra = models.FloatField()
    entrada = models.FloatField()
    data_compra = models.DateField()
    tipo_parcelamento = models.IntegerField()
    quantidade_parcelas = models.IntegerField()
    data_criacao = models.DateTimeField(auto_now=True)
    
    @property
    def valor_parcela(self):
        valor_parcelado = self.valor_compra - self.entrada
        return valor_parcelado / self.quantidade_parcelas

    def data_parcelas(self):
        nextmonth = self.data_compra + relativedelta.relativedelta(months=1)
        parcelas_info = {}
        for x in range(1, self.quantidade_parcelas + 1):
            parcelas_info[i] = nextmonth
            nextmonth = nextmonth + relativedelta.relativedelta(months=1)
        return parcelas_info

That way, when creating a purchase you can access the date of each installment as follows:

nova_compra = Compras.objects.create(nome_compra=nome_compra,...)
data_parcelas = nova_compra.data_parcelas()

for numero_da_parcela in data_parcelas:
    data_parcelas[numero_da_parcela]

You can then access what is the number of that plot, the date it will occur, and through the nova_compra.valor_parcela the value of each tranche.

  • Thank you very much, our arrival gave me a lively your reply, I will be changing my code to carry out the changes you gave me. Thank you very much So answering your question regarding variable installation_type, which the user inform will have 3 types of installments, daily (input + 1 installment), fortnightly (15 days to pay) and monthly (default)then I’ll calculate the benefits on top of that information.

Browser other questions tagged

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