this is another UnicodeDecodeError I'm dealing with in django. I can't find the way to solve it.
I'm trying to create an object:
nivel_obj = Nivel.objects.filter(id=nivel_id)
nueva_matricula = Matricula(nivel=nivel_obj, ano_lectivo=ano_lectivo, alumno=a)
nueva_matricula.save()
The "Matricula" object has a "nivel_obj" that is a Foreign Key. The "nivel_obj" has a name that is a string that could not be encoded/decoded.
How can I solve this?
These are the models:
class Nivel(models.Model):
"""
Ej - "Octavo de Basica, 6to Curso"
"""
nombre = models.CharField(max_length=150)
class Meta:
verbose_name_plural = "niveles"
def __unicode__(self):
return u"%s" % (self.nombre)
class Matricula(models.Model):
ano_lectivo = models.PositiveIntegerField(validators=[MaxValueValidator(9999)])
alumno = models.ForeignKey(Alumno)
nivel = models.ForeignKey(Nivel, null=True) <----
status = models.CharField(max_length=150, choices=(("A", "Activo"), ("I", "Inactivo")))
def validate_unique(self, exclude=None):
if Matricula.objects.filter(alumno=self.alumno, nivel=self.nivel, ano_lectivo=self.ano_lectivo).exists():
error = u'Ya existe una matrícula igual, por favor revisa el año, el nivel y el alumno'
raise ValidationError({NON_FIELD_ERRORS: error})
else:
pass
class Meta:
verbose_name_plural = "matrículas"
verbose_name = "matrícula"
ordering = ("alumno",)
def __unicode__(self):
return u"Matricula %s %s" % (self.alumno, self.ano_lectivo)
The exact error comes from a "Nivel" object that has a name like this "Octavo de Básica", I can't work with it without having a UnicodeDecodeError.
This is the error:
UnicodeDecodeError at /sisacademico/matricular_grupo/
'ascii' codec can't decode byte 0xc3 in position 20: ordinal not in range(128)
...
The string that could not be encoded/decoded was: de B��sica
EDIT: Found the Error
OK I found my error, I'm not going to delete the question cause the error (UnicodeDecodeError) django was giving me is completely misleading. The error was this one:
nivel_obj = Nivel.objects.filter(id=nivel_id) <---
nueva_matricula = Matricula(nivel=nivel_obj, ano_lectivo=ano_lectivo, alumno=a)
I cannot save a new object with a nivel=queryset and not nivel=NivelObject.
it should be:
nivel_obj = Nivel.objects.get(id=nivel_id)
My mistake.
BUT, WHY ON EARTH would django give me a UnicodeDecodeError?!!
verbose_nameattributes onMatricula, egverbose_name = u'matrícula', but I don't see why that would create the exception you're seeing.UnicodeDecodeErrorcomes from trying to mix encoded strings and Unicode objects when the encoded string contains non-ascii characters, eg'matr\xc3\xadcula' + u'matrícula', so something somewhere is getting an encoded version of the name. Django doesn't do that on its own - I or other readers might be able to help spot it if you can show your view code.