-1

I have an artist which has many paintings, so a many to one relation. So i got

class Artist(models.Model):
    name   = models.CharField(max_length=120)
    age    = models.IntField()

class Paintings(models.Model):
    painting         = models.ImageField()
    painting_name    = models.CharField(max_length=120, default='', null=True)
    artist = models.ForeignKey(
       'Artist',
       related_name='paintings',
       on_delete=models.CASCADE,
       verbose_name= 'Künstler'
    )

Now if I'll create a new Painting rely to an artist it doesnt show me the exact name of the artist, just the artist object [id]artist object [id]

inside my admin.py I try to define

artist = Artist.name 
list_display = ('image_tag', artist, 'painting_name')

but this is not allowed. So how can I replace the Artist Object (id) with the actual name of the artist?

DarK_FirefoX
  • 887
  • 8
  • 20
Dariun
  • 327
  • 3
  • 14
  • override `__str__` in `Artist` model, it simply tells django how to print and convert the object to a string – Sajad Mar 30 '20 at 14:12

1 Answers1

1

In your Artist model you must define a __str__ function:

def __str__(self):
  return self.name # Or whatever way you want your Artist to show

This tells Django how to to print that object.

From the Django Documentation:

__str__()

The __str__() method is called whenever you call str() on an object. Django uses str(obj) in a number of places. Most notably, to display an object in the Django admin site and as the value inserted into a template when it displays an object. Thus, you should always return a nice, human-readable representation of the model from the __str__() method.

DarK_FirefoX
  • 887
  • 8
  • 20