1đź‘Ť
Change __unicode__
to __str__
method and see. It should work with python 3.
class Departement(models.Model):
departNumber = models.PositiveSmallIntegerField(default=0, primary_key= True)
departName = models.CharField(max_length=100)
def __str__(self):
return self.departNumber
to make it compatible with python 2 you can do what’s suggested here:
https://docs.djangoproject.com/en/1.10/ref/models/instances/
0đź‘Ť
You say:
Departement.objects.filter(departNumber=2)
should give me a departName
that is “Var” since “Var” is the department with 2 as a primary key.
So you are relying on the __unicode__
method to return the department name (even though your implementation of the __unicode__
method currently returns the department number).
Why not access the attribute directly?
dept = Departement.objects.filter(departNumber=2)
dept.departName
It makes for more maintainable code in the long run.
But to answer the why it’s not working we need to know if you are using python 2 or 3.
If 3 then change the name of the method to __str__
as described here: Why __unicode__
doesn’t work but __str__
does?
- Heroku can't find pip when I push app to it
- Django – registration login.html as index
- Passing list or array (as parameter) in Django View