In the view where I show the records, among other fields I want to show one that is “estado_cod_id”
I assign this field at the time of registering and assign it 1
The state field is related to another table where the different states are found.
The problem is that when displaying the records, the estado_cod_id field appears as undefined but looking at the database I see that it was added correctly and is related to the estado_cod_id table.
I show parts of code so you can see how I do it.
Views.py
class RegistrarReclamacion(CreateView):
model = Rec
form_class = RecForm
template_name = 'reclamaciones/crear_reclamacion.html'
def post(self, request, *args, **kwargs):
request.headers.get('x-requested-with') == 'XMLHttpRequest'
if request.headers.get('x-requested-with') == 'XMLHttpRequest':
form = self.form_class(request.POST)
if form.is_valid():
nueva_reclamacion = Rec(
fecha_reclamacion = form.cleaned_data.get('fecha_reclamacion'),
num_reclamacion = form.cleaned_data.get('num_reclamacion'),
num_cliente = form.cleaned_data.get('num_cliente'),
empresa = form.cleaned_data.get('empresa'),
tipo = form.cleaned_data.get('tipo'),
motivo = form.cleaned_data.get('motivo'),
descripcion = form.cleaned_data.get('descripcion'),
solucion = form.cleaned_data.get('solucion'),
otros_solucion = form.cleaned_data.get('otros_solucion'),
aprueba = form.cleaned_data.get('aprueba'),
del_dep_code = form.cleaned_data.get('del_dep_code'),
)
#nueva_reclamacion.save()
nueva_reclamacion = form.save(commit=False)
nueva_reclamacion.estado_cod_id = "1"
nueva_reclamacion.save()
form.save_m2m()
mensaje = f'{self.model.__name__} registrada reclamacion correctamente!'
error = 'No hay error'
response = JsonResponse({'mensaje':mensaje,'error':error})
response.status_code = 201
return response
else:
mensaje = f'{self.model.__name__} no se ha podido registrar!'
error = form.errors
response = JsonResponse({'mensaje':mensaje,'error':error})
response.status_code = 400
return response
else:
return redirect('inicio_reclamaciones')
models.py
class Sat_estados(models.Model):
codigo = models.IntegerField(verbose_name='codigo')
nombre = models.CharField(max_length=50, verbose_name='Nombre')
descripcion = models.CharField(max_length=100, verbose_name='Descripcion')
class Rec(models.Model):
id = models.AutoField(primary_key=True)
estado = models.IntegerField(verbose_name='estado', null=True)
fecha_reclamacion = models.DateField(null=True,)
fecha_resolucion = models.DateField(verbose_name='Fecha_resolucion', null=True)
num_reclamacion = models.CharField(max_length=100, null=True)
num_cliente = models.CharField(max_length=100, null=True)
empresa = models.CharField(max_length=100, null=True)
tipo = models.CharField(max_length=100,null=False,blank=False,choices=TIPO_EMPRESA,default='')
atiende = models.CharField(max_length=100, null=True,)
atiende_code = models.IntegerField(verbose_name='Atiende_code', null=True)
departamento = models.CharField(max_length=100, null=True)
del_dep_code = models.CharField(max_length=100, null=True)
via = models.CharField(max_length=100,null=False,blank=False,choices=VIA,default='')
motivo = models.CharField(max_length=100,null=False,blank=False,choices=MOTIVO_RECLAMACION,default='')
otros_motivo = models.CharField(max_length=100, verbose_name='Otros Motivos', null=True)
descripcion = models.TextField(max_length=100, verbose_name='Descripcion', null=True)
solucion = models.CharField(max_length=100,null=False,blank=False,choices=RESOLUCION,default='')
otros_solucion = models.CharField(max_length=100, verbose_name='Otros Solucion', null=True)
aprueba = models.CharField(max_length=100, verbose_name='Aprueba', null=True)
aprueba_codigo = models.IntegerField(verbose_name='Aaprueba_codigo', null=True)
aprueba_selected = models.CharField(max_length=100, verbose_name='Aprueba_selected', null=True)
imagen = models.ImageField(upload_to='imagenes/', verbose_name='Imagen', null=True)
estado_cod = models.ForeignKey(Sat_estados, null=True,blank=True,on_delete=models.CASCADE)