LuisFerchx
LuisFerchx

Reputation: 47

ValueError: Cannot assign must be a instance

Because of a method to obtain the choices that I want to add to a field, it is giving me this error when saving the form.

ValueError: Cannot assign "'17'": "Con_Transaccioncab.doc" must be a "Adm_Documento" instance.

As I read in this post ValueError: Cannot assign object must be a instance the value is not being returned to me as an object, what other way can I do to retrieve the values ​​in my get_choices method?

get_choices method

# Select de 3 tablas para obtener el nombre y valor del documento de la tabla adm_documento_periodo
    def get_choices(self):
        all_tipoaux = Adm_DocumentoPeriodo.objects.select_related('doc').filter \
            (per=self.AIGN_PER_ID).select_related('doc__mod').filter(doc__mod__mod_codigov='CON').values("doc__doc_id",
                                                                                                         "doc__doc_nombre")
        DOC = [(d['doc__doc_id'], d['doc__doc_nombre']) for d in all_tipoaux]
        return DOC

and this way I place it in the choicefield inside my form:

self.fields['doc'] = ChoiceField(label='Acción: ', choices=self.get_choices(), required=False)

any suggestion is welcome and appreciated

Models.py

class Adm_Documento(ERPBaseModel):
    class Choices_DocTipo(models.TextChoices):
        Ingreso = 'I', _('Ingreso')
        Egreso = 'E', _('Egreso')
        Transferencia = 'T', _('Transferencia')
        EntregaPedido = 'P', _('Entrega pedido')

    class Choices_DocMovDebCre(models.TextChoices):
        Cheque = 'CHE', _('Cheque')
        Deposito = 'DEP', _('Depósito')
        NotaCredito = 'NDC', _('Transferencia')
        NotaDebito = 'NDD', _('Entrega pedido')
        Ninguno = '', _('Ninguno')

    doc_id = models.BigAutoField(primary_key=True)
    doc_codigov = models.CharField(max_length=32, verbose_name='Código')
    doc_nombre = models.CharField(max_length=64, verbose_name='Nombre')
    doc_tipo = models.CharField(max_length=1, blank=True, null=True, verbose_name='Tipo',
                                choices=Choices_DocTipo.choices)
    doc_orden = models.SmallIntegerField(verbose_name='Orden')
    doc_actcosto = models.BooleanField(verbose_name='Actualiza costo')
    doc_movcre = models.CharField(max_length=3, blank=True, null=True, verbose_name='Mov. Crédito',
                                  choices=Choices_DocMovDebCre.choices, default=Choices_DocMovDebCre.Ninguno)
    doc_movdeb = models.CharField(max_length=3, blank=True, null=True, verbose_name='Mov. Débito',
                                  choices=Choices_DocMovDebCre.choices, default=Choices_DocMovDebCre.Ninguno)
    tcc = models.ForeignKey(Sis_TemplateContabCab, models.DO_NOTHING, blank=True, null=True,
                            verbose_name=Sis_TemplateContabCab._meta.verbose_name)
    mod = models.ForeignKey(Sis_Modulo, models.DO_NOTHING, verbose_name=Sis_Modulo._meta.verbose_name)
    emp = models.ForeignKey(Adm_Empresa, models.DO_NOTHING, verbose_name=Adm_Empresa._meta.verbose_name)
    doc_estado = models.SmallIntegerField(default=True)

    class Meta:
        managed = False
        db_table = 'adm\".\"documento'
        unique_together = (('doc_id', 'emp'), ('doc_codigov', 'emp'))
        verbose_name = 'Documento'
        verbose_name_plural = 'Documentos'

    def __str__(self):
        return self.doc_nombre


class Adm_DocumentoPeriodo(ERPBaseModel):
    dop_id = models.BigAutoField(primary_key=True)
    doc = models.ForeignKey(Adm_Documento, models.DO_NOTHING,db_column='doc_id')
    per = models.ForeignKey(Adm_Periodo, models.DO_NOTHING,db_column='per_id')
    emp = models.ForeignKey(Adm_Empresa, models.DO_NOTHING,db_column='emp_id')
    dop_secuencia = models.BigIntegerField(verbose_name='Secuencia')
    dop_estado = models.SmallIntegerField(default=True)

    class Meta:
        managed = False
        db_table = 'adm\".\"documento_periodo'
        unique_together = (('per', 'emp', 'doc'),)

class Sis_Modulo(models.Model):
    mod_id = models.BigAutoField(primary_key=True)
    mod_codigov = models.CharField(max_length=8)
    mod_nombre = models.CharField(max_length=32)
    mod_creadoc = models.BooleanField()
    mod_estado = models.SmallIntegerField()

    def __str__(self):
        return self.mod_nombre

    class Meta:
        managed = False
        db_table = 'sis\".\"modulo'
        verbose_name = 'Módulo'

Upvotes: 0

Views: 905

Answers (1)

chickahoona
chickahoona

Reputation: 2034

Change per=self.AIGN_PER_ID to per_id=self.AIGN_PER_ID

Upvotes: 1

Related Questions