[Answered ]-Using Relationship Based Data in Model Definition – Django

1👍

You can access the related Publisher instance through the ForeignKey field and get the publisherName in the __str__() method so:

def __str__(self):
    publisher_name = self.publisher.publisherName
    return f'{self.pencilerName} ({publisher_name})'

Additionally, I’d recommend you to use string formatting, such as using f-strings.

0👍

It is as simple as that:

def __str__(self):
    return f"{self.pencilerName} ({self.publisher})"

Leave a comment