1

在这种情况下如何只获取最新的对象?

{% 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'),
)
4

1 回答 1

1
instance.event.myevent_set.all.0

或者

instance.event.myevent_set.all|first
于 2013-09-26T11:38:55.630 回答