在这种情况下如何只获取最新的对象?
{% for entry in instance.event.myevent_set.all %}
....
{% endfor %}
我的尝试:
{% for entry in instance.event.myevent_set.all[0] %}
....
{% endfor %}
但当然不行。
模型:
class Event(models.Model):
title = models.CharField(max_length=200)
class MyEvent(models.Model):
title = models.CharField(max_length=200)
event = models.ForeignKey(Event)
和我的网址:
urlpatterns = patterns('',
url(r'^$',
ListView.as_view(
queryset=MyEvent.objects.all(),
context_object_name='latest_entry_list',
template_name="blog/index.html"
),
name="blog_list"),
url(r'^(?P<id>\d+)$', 'event.views.single_event'),
)
答案 0 :(得分:1)
instance.event.myevent_set.all.0
或
instance.event.myevent_set.all|first