Django : how to display actual objects in admin

I have a model defination as follows:

class Artist(models.Model):
    """Model class to record Artist"""
    artist_name = models.CharField(max_length=200)
    artist_id = models.CharField(unique=True, max_length=50)
    # similar relationship is defined.
    related_to = models.ManyToManyField("self", null=True, blank=True)

    def __unicode__(self):
        return self.artist_name

when i use 'delete' admin action for the artists from django' admin panel i get:

Artist: trojan

    From_artist-to_artist relationship: Artist_related_to object
    From_artist-to_artist relationship: Artist_related_to object
    From_artist-to_artist relationship: Artist_related_to object
    From_artist-to_artist relationship: Artist_related_to object

Artist: betty

    From_artist-to_artist relationship: Artist_related_to object
    From_artist-to_artist relationship: Artist_related_to object

how do i get the actual relations that i am warned during delete?

in other words how to humanize 'Artist_related_to object' to reflect actual artist name.

Answers


What you are seeing in the admin delete question are instances of the ManyToMany bridge model that Django transparently creates for you.

If you want to change their __unicode__ representation you will have to define your own bridge model and set the the ManyToManyField to use it with the through parameter.

Example:

class ArtistRelationship(models.Model):
    from_artist = models.ForeignKey('Artist')
    to_artist = models.ForeignKey('Artist')

    def __unicode__(self):
        return u"%s - %s" % (self.from_artist.name, self.to_artist.name)


class Artist(models.Model):
    name = models.CharField(max_length=200)
    # ... more fields ...
    related_to = models.ManyToManyField("self", null=True, blank=True,
                                        through=ArtistRelationship)

Need Your Help

Azure Stream Analytics and Power BI

azure powerbi azure-stream-analytics

I have an Azure Stream Analytics job outputting to PowerBI - outputs are present and the data set is present in PowerBI - however, the graph is not live as I have to manually refresh the page to re...

How to select a date range for current and last year in Python

python datetime pandas slice date-range

I am trying to select data from a series between two dates. It's working alright using df.loc and mask but I want to make it automatically get current year data and last year data without entering it