How do I define the str method for a foreignkey field in Django? The code below does not work,
class A(models.Model):
name = models.ForeignKey('B')
def __str__(self):
return self.name
If your model B
has a name
field, you could:
def __str__(self):
return self.name.name
Or you could try define the __str__
method in B
model.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With