这是我遇到的一个非常普通的问题,到目前为止,在阅读Django文档或本网站上的任何其他一些相关回复之后,我都无法进一步调试此问题。
我的问题是这个,我将发布代码,以便人们也可以看到我正在尝试完成的工作。
我有以下型号
class Tournament(models.Model):
name = models.CharField(max_length=128, null=True)
description = models.CharField(max_length=2000, null=True)
number_players = models.IntegerField(default=-1)
teams_per_game = models.IntegerField(default=-1)
template = models.IntegerField(default=-1)
created_by = models.ForeignKey('Player')
created_date = models.DateTimeField(auto_now=True)
number_rounds = models.IntegerField(default=-1)
is_finished = models.BooleanField(default=False)
has_started = models.BooleanField(default=False)
players_per_team = models.IntegerField(default=1)
private = models.BooleanField(default=False,db_index=True)
start_option_when_full = models.BooleanField(default=True)
host_sets_tourney = models.BooleanField(default=False)
max_players = models.IntegerField(default=-1)
template_settings = models.TextField()
@property
def time_since_created(self):
print("Here!")
fmt = '%H:%M:%S'
td = datetime.strptime(datetime.now(), fmt) - datetime.strptime(self.created_date, fmt)
vars(td)
print ("Hours: {}".format(td.hours))
print ("Minutes: {}".format(td.minutes))
print ("Seconds: {}".format(td.seconds))
if td.hours > 0:
return td.hours + " hours ago"
if td.minutes > 0:
return td.minutes + " minutes ago"
if td.seconds > 0:
return td.seconds + " seconds ago"
def __str__(self):
return "Tournament: " + self.name
class SwissTournament(Tournament):
type = models.CharField(max_length=255, default="Swiss")
max_rating = models.IntegerField(default=0)
min_rating = models.IntegerField(default=0)
best_record = models.CharField(max_length=10, null=True)
max_teams = 128
min_teams = 16
当我这样做时:
def index(request):
context = {}
try:
swiss_tournaments = SwissTournament.objects.filter(private=False).values()[:20]
if swiss_tournaments.count() > 0:
context.update({'swiss_tournaments': swiss_tournaments})
#for t in swiss_tournaments:
# calling time_since_created set's time_since in our objects
return render(request, 'index.html', context)
except ObjectDoesNotExist:
log("No open tournaments", LogLevel.informational)
return render(request, 'index.html', context)
我将瑞士锦标赛的对象归零,并且可以显示time_since_created之外的所有属性……我什至在代码框的标准输出中都看不到print语句,就像我在代码中的其他所有地方一样。
模板代码:
{% for swiss_t in swiss_tournaments %}
<div class="card-body">
<a class="card-link" href="#">
<span class="card-title h5"> {{ swiss_t.name }} </span><span class="text-muted h7 mb-2"> <i class="fa fa-clock-o"></i>{{ swiss_t.time_since_created }}</span></span>
</a>
<p class="card-text">
{{ swiss_t.description }}
</p>
<div>
<span class="badge badge-primary">{{ swiss_t.created_by.name }}</span>
<a href="https://warzone.com/MultiPlayer?TemplateID={{ swiss_t.template }}" target="_blank"><span class="badge badge-primary">Template {{ swiss_t.template }}</span></a>
<span class="badge badge-primary">Max Players: {{ swiss_t.max_players }}</span>
<span class="badge badge-primary">Node.js</span>
<span class="badge badge-primary">Ruby</span>
<span class="badge badge-primary">Paython</span>
</div>
</div>
{% endfor %}
我什至尝试过创建一个子类@property并引用父类(或在那里计算它)……不走运,这个方法永远不会被调用。
有人知道我可以做些什么来进一步调试吗?或者有任何想法,一旦对象被加载,我该如何在视图中引用这样的@property?
答案 0 :(得分:0)
您在查询中使用了values()
方法,这意味着您不是在向模板发送模型实例的查询集,而是在发送字典列表。因此,各个对象没有模型上定义的任何方法。
除非有充分的理由(除非您不在这里),否则应避免使用values
。使用实际的查询集:
swiss_tournaments = SwissTournament.objects.filter(private=False)[:20]