[Fixed]-Cannot get model objects to display attributes in the django admin pages

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/

👤Vikash Singh

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?

👤Tony

Leave a comment