Francisco Ghelfi
Francisco Ghelfi

Reputation: 962

Django: only 1 field from model not updating

I have a model with companies profiles, a model form and a page where I show the form and can edit it.

When I change the data in the form all the fields get updated correctly except 1: "NombrePcia".

It´s just a charfiled and there is no filtering around that value. I can´t understand why the update is missing just this filed.

Thanks in advance.

The model

class Contactos(models.Model):
    codigo = models.IntegerField(help_text=" Código ", blank=True, null=True)
    Nombre = models.CharField(max_length=200, help_text=" Nombre ", blank=False, null=True)
    Domicilio = models.CharField(max_length=200, help_text=" Domicilio ", blank=True, null=True)
    Localidad = models.CharField(max_length=200, help_text=" Localidad ", blank=True, null=True)
    CodPostal = models.IntegerField(help_text=" Cod.Postal ", blank=True, null=True)
    PR = models.IntegerField(help_text=" PR ", blank=True, null=True)
    Razonsociallegal = models.CharField(max_length=200, help_text=" Razón.social.legal ", blank=True, null=True)
    Telefonos = models.CharField(max_length=200, help_text=" Teléfonos ", blank=True, null=True)
    NroDoc = models.DecimalField(help_text=" Nro.Doc. ", decimal_places=2, max_digits=100, blank=True, null=True)
    TD = models.IntegerField(help_text=" TD ", blank=True, null=True)
    SituacionIVA = models.CharField(max_length=200, help_text=" Situación.IVA ", blank=True, null=True)
    NombrePcia = models.CharField(max_length=200, help_text=" NombrePcia ", blank=True, null=True)
    emailEmpresa = models.CharField(max_length=200, help_text=" email.Empresa ", blank=True, null=True)
    Agrupacion1 = models.ForeignKey("Conceptos_Clientes", help_text="Concepto cliente", blank=True, null=True, on_delete=models.CASCADE)
    Agrupacion2 = models.IntegerField(help_text=" Agrupacion2 ", blank=True, null=True)
    BancoCBU = models.DecimalField(help_text=" BancoCBU ", decimal_places=2, max_digits=100, blank=True, null=True)
    CodigoZona = models.IntegerField(help_text=" CodigoZona ", blank=True, null=True)
    Comentario1 = models.CharField(max_length=200, help_text=" Comentario1 ", blank=True, null=True)
    Comentario2 = models.CharField(max_length=200, help_text=" Comentario2 ", blank=True, null=True)
    Concepto1 = models.IntegerField(help_text=" Concepto1 ", blank=True, null=True)
    CpCliente = models.IntegerField(help_text=" CpCliente ", blank=True, null=True)
    Descuento1 = models.IntegerField(help_text=" Descuento1 ", blank=True, null=True)
    EsCliente = models.CharField(max_length=200, help_text=" EsCliente ", blank=True, null=True)
    EsVendedor = models.CharField(max_length=200, help_text=" EsVendedor ", blank=True, null=True)
    Fax = models.CharField(max_length=200, help_text=" Fax ", blank=True, null=True)
    FechaAlta = models.DateField(help_text="Fecha de alta", default=datetime.date.today, blank=True, null=True)
    FechaModi = models.DateField(help_text="Fecha de modificación", blank=True, null=True)
    FechaUltOp = models.DateField(help_text="Fecha última operación", blank=True, null=True)
    FechaUltVenta = models.DateField(help_text="Fecha última venta", blank=True, null=True)
    IIBBCMJurisSede = models.IntegerField(help_text=" IIBBCMJurisSede ", blank=True, null=True)
    IIBBNroEsCUIT = models.CharField(max_length=200, help_text=" IIBBNroEsCUIT ", blank=True, null=True)
    IIBBSituacion = models.CharField(max_length=200, help_text=" IIBBSituacion ", blank=True, null=True)
    Lista = models.IntegerField(help_text=" Lista ", blank=True, null=True)
    MTCategoria = models.CharField(max_length=200, help_text=" MTCategoria ", blank=True, null=True)
    PorcLiberacion = models.IntegerField(help_text=" PorcLiberacion ", blank=True, null=True)
    ProFAREBasePend = models.CharField(max_length=200, help_text=" ProFAREBasePend ", blank=True, null=True)
    PuntosDeVenta = models.IntegerField(help_text=" PuntosDeVenta ", blank=True, null=True)
    SituacionEmpleador = models.CharField(max_length=200, help_text=" SituacionEmpleador ", blank=True, null=True)
    SituacionGanancias = models.IntegerField(help_text=" SituacionGanancias ", blank=True, null=True)
    Vendedor = models.ForeignKey("Vendedores", help_text="Estatus del contenido", blank=True, null=True, on_delete=models.CASCADE)

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

The form

class EmpresaForm(forms.ModelForm):
    class Meta:
        model = Contactos
        fields = ("Vendedor", "codigo", 'Nombre', 'Razonsociallegal',
                  "NombrePcia", "Localidad", "Domicilio", "CodPostal", "CodigoZona",
                  "Telefonos", "emailEmpresa", "Fax",
                  "Descuento1", "NroDoc", "SituacionIVA", "BancoCBU", "SituacionEmpleador", "SituacionGanancias",
                  "Comentario1", "Comentario2", "Concepto1",
                  "Agrupacion1", "Agrupacion2", "EsCliente", "EsVendedor",
                  "FechaAlta", "FechaModi", "FechaUltOp", "FechaUltVenta",
                  "IIBBCMJurisSede", "IIBBNroEsCUIT", "IIBBSituacion", "Lista",
                  "MTCategoria", "PorcLiberacion", "ProFAREBasePend", "PuntosDeVenta",
                  "PR", "TD", "CpCliente")

The view

def PerfilClientesView(request, empresa_id):
    empresa_id = empresa_id
    contactos = Contactos.objects.get(pk=empresa_id)

    if request.method == "POST":
        perfil_cliente_form = EmpresaForm(request.POST, instance=contactos)
        if perfil_cliente_form.is_valid():
            cliente = perfil_cliente_form.save(commit=False)
            cliente.FechaModi = date.today()
            cliente.save(force_update=True)

    perfil_cliente_form = EmpresaForm(initial={}, instance=contactos)

    pre_email_form = {"remitente": request.user.email, "destinatario": contactos.emailEmpresa}
    email_form = EmailForm(pre_email_form)

    ventas = Ventas.objects.filter(cliente__icontains=contactos.Nombre)
    ventas_cliente = ventas.values('cliente').annotate(fact_total=Sum('subtotal'))
    ranking_clientes = ventas_cliente.order_by('-fact_total')

    pedidos_cliente = ventas.filter(comprobante__icontains="FA")
    pedidos_cliente = pedidos_cliente.values('cliente').annotate(
        total_pedidos=Count('comprobante', distinct=True))

    operaciones_cliente = ventas.values('cliente', 'comprobante', 'fecha').annotate(fact_operación=Sum('subtotal')).order_by('-fecha')

    conceptos_clientes = Conceptos_Clientes.objects.all()

    return render(request, 'catalog/PerfilCliente.html', {
        'conceptos_clientes': conceptos_clientes,
        'perfil_cliente_form': perfil_cliente_form,
        'ventas_cliente': ventas_cliente,
        'ranking_clientes': ranking_clientes,
        'pedidos_cliente': pedidos_cliente,
        'empresa_id': empresa_id,
        'operaciones_cliente': operaciones_cliente,
        'ventas': ventas,
        'contactos': contactos,
        'email_form': email_form,
        'pre_email_form': pre_email_form,

    })

Edit

In the template I had:

<div style="margin-bottom: 0.5rem;">{{ perfil_cliente_form.NombrePcia|title }}</div>

When I removed the |title part of the statement it worked alright. What I still don´t understand is why this title decorator is afecting this field in special but no other fields (also charfields) where I also use it as:

<div style="margin-bottom: 0.5rem;">{{ perfil_cliente_form.Localidad|title }}</div>

Upvotes: 0

Views: 349

Answers (1)

Daniel Holmes
Daniel Holmes

Reputation: 2002

The docs describe the title template tag as follows:

Converts a string into titlecase by making words start with an uppercase character and the remaining characters lowercase.

Using the title for a form field and not a plain string may be the reason why you are getting inconsistent behaviour. If you would like to have more control over how you display your forms, you can take a look at something like django-crispy-forms.

Upvotes: 1

Related Questions