1👍
✅
unique_together
isn’t a compound primary key. It’s just a compound constraint. Your model still has a hidden, autogenerated id
field which is the real primary key.
Yes, you can set up __str__
to output whatever you want so long as it’s a string. It doesn’t have to be unique, but it really helps if it is. (BTW, I’m not sure which Python you’re using or if this changes in Python 3, but it’s recommended to use __unicode__
instead of __str__
.)
def __unicode__(self):
return "{0} (from {1})".format(self.name, self.supplier)
But again, this isn’t your actual primary key. To see that as well (not really recommended, because it’s noise):
def __unicode__(self):
return "{0} (from {1}) (#{2})".format(self.name, self.supplier, self.id)
Source:stackexchange.com