user7804233
user7804233

Reputation:

Django foreign key name on select

I'm working with 2 models, each one in a different Django application:

App products:

class Product(models.Model):
    name = models.CharField(max_length=256)
    price = models.FloatField()
    warehouse = models.ForeignKey(Warehouse, on_delete=models.CASCADE)

    def __unicode__(self):
        return self.name

App warehouses:

class Warehouse(models.Model):
    name = models.CharField(max_length=256)

    def __unicode__(self):
        return self.name

The problem is that in the admin site, when I want to create a product and I need to select a warehouse, I only see the following in the select list:

Warehouse object
Warehouse object
Warehouse object

What do I have to do to see the warehouse name?

Upvotes: 0

Views: 587

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599926

The __unicode__ method is only used in Python 2. Since you are presumably using Python 3, you should define __str__ instead.

Upvotes: 3

Related Questions