New to python, and trying to use __unicode_ to render a string representation. The code is part of Django model. How can I write __unicode__ of MyType so that in templates it output its representation as 123 - 123 South ....
class UsAddress(models.Model):
#other fields
zip = us_models.USPostalCodeField()
country = models.CharField(max_length=2)
phone = us_models.PhoneNumberField()
def __unicode__(self):
return self.zip + self.country + self.phone
class MyType(models.Model):
code = models.IntegerField()
address = UsAddress
def __unicode__(self):
return str(self.code) + " - " + unicode(self.address) #self.address.__unicode__()
Output:
<MyType: 219 - <class 'web.models.UsAddress'>>
EDIT
At least in my case, the problem was I didn’t model the relationship. So I added it to UsAddress.
mt = models.ForeignKey(MyType)
Try this:
Here is the unicode method.
Then, you can just use it in your template like this:
The output will be: