0

I have a model/table, WeekOne with various fields. It also has a unicode field:

def __unicode__(self):
    return u'Week One'

WeekOne is a foreign key of user, defined using the variable weekOne. Thinking I could leverage the unicode field in a template, I tried to display the name using the following code:

{{ user.weekOne }}

Nothing showed up. It's not an issue of my models functioning properly, I access other fields in model/weekOne throughout the template without issue. What's the best way to display the name of a table? Do I need to create a "Name" field? That seems like kind of a waste, since every user would have a WeekOne table with the redundant "Name" field.

Here is the model of UserProfile, which is what the user variable references:

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    weekOne = models.OneToOneField(WeekOne)

When I do something like user.weekOne.items (items is a field in weekOne), it shows up on the template.

4

1 回答 1

2

尝试这个

{{ user.userprofile.weekOne }}

PS:不要将模型称为表。模型是一个类。它用于与数据库表对话。表名和模型名可以不同。

于 2013-03-25T16:29:58.493 回答