user3895077
user3895077

Reputation:

why django model instance naming object itself

I don`t know this the right title to ask. And i have not enough reputation to post image.

models.py

class Artist(models.Model):
    artist_name = models.CharField(max_length=100)

class Album(models.Model):
    album_name = models.CharField(max_length=100)
    artist = models.ForeignKey(Artist)

And i registered models to admin

in the admin page i added artist name no issue. Then in the album section in the drop down it`s showing like album object instead of what i given the artist name. what is that for ?

Upvotes: 0

Views: 56

Answers (2)

petkostas
petkostas

Reputation: 7460

Even better if you want to be in sync with python 2 and 3:

At the top of your models:

from __future__ import unicode_literals

then before your class:

@python_2_unicode_compatible
class YourClass(models.Model):

And then:

    def __str__(self):
        return '%s' % self.title

Upvotes: 1

rczajka
rczajka

Reputation: 1840

Django doesn't know you want to use artist_name as a string representation of the Artist object. You need to add a __unicode__ (or __str__ on Python 3) method to your Artist model:

def __unicode__(self):
    return self.artist_name

Upvotes: 2

Related Questions