2011-03-05 240 views
11

這是我的代碼。如何獲取第一個元素和最後一個元素使用django,Location.objects.all()

obj_list=Location.objects.all() 
first_element=obj_list[0] 
last_element=obj_list[-1] 

然後,

return render_to_response(template_name, { 
     'first_element':first_element, 
     'last_element':last_element, 
    }) 

,並在模板:

{{ first_element.terminal_id}} {{last_element.terminal_id}} 

,但它什麼都不顯示,

我能做些什麼,

感謝

+0

如果負面索引是問題,first_element應該至少顯示一些內容,您在什麼程度上調試了這個? – gladysbixly 2011-03-05 10:11:21

+0

如果您找到了解決方案,請更新並關閉此問題。 – zsquare 2012-05-04 18:49:30

回答

19

看一看http://docs.djangoproject.com/en/dev/topics/db/queries/#limiting-querysets

負索引(即Entry.objects.all()[-1])不支持。

嘗試:

first_element = Location.objects.all()[0] 
last_element = Location.objects.all().reverse()[0] 

- 更新17年8月6日 -

基於@MisterRios'評論,

1.6的Django的支持使用.first().last()查詢集: first_element = Location.objects.first() last_element = Location.objects.last()

請參閱:https://docs.djangoproject.com/en/1.7/ref/models/querysets/#django.db.models.query.QuerySet.first

+0

從1.6開始,Django支持在查詢集上使用'.first()'和'.last()':last_element = Location.objects.first()' 'last_element = Location.objects.last()' See: https://docs.djangoproject.com/zh/dev/ref/models/querysets/#django.db.models.query.QuerySet.first – MisterRios 2017-04-12 12:19:03

+0

謝謝!更新答案。 – zsquare 2017-06-08 06:22:20

2

你可能不能夠負索引查詢集,但可以將該查詢集放入列表中,然後將其索引。

locations = list(Location.objects.all()) 
first_element = locations[0] 
last_element = locations[-1] 

這是可怕的,雖然效率不高,如果有在你的表位置少數,才應使用,並且要保持代碼的簡潔。否則,如果真的需要這樣做,請參閱@ pterk的答案,涉及聚集和Min/Max。

1

最後: - Location.objects.reverse()[0]

OR

  Location.objects.all()[Location.objects.count()-1] // BAD WAY 

第一:Location.objects.all()[0]

注:不支持負索引。所以,Location.objects.all()[-1]將拋出你的 Asse田

+2

-1用於誤導人 - .all()[1]是* second *元素,而不是第一個。 – 2011-03-05 11:31:59

+0

@Tomasz,感謝您的糾正....我的錯誤:( – Tauquir 2011-03-05 21:09:29

0

如果任何人來到這裏獲得的第一個和最後一個項目相關模型 - 有效地做到這一點的唯一方法是將相關字段轉換爲列表或使用count()來獲取最後一項的索引(使用Django 1.11.2):

class Parent(models.Model): 
    name = models.CharField(max_length=200) 


class Child(models.Model): 
    parent = models.ForeignKey(Parent, on_delete=models.CASCADE, related_name='children') 
    name = models.CharField(max_length=200) 


class ParentTest(TestCase): 
    def test_parent(self): 
     # create some data 
     for p in range(10): 
      parent = Parent.objects.create(name=p) 
      for c in range(10): 
       parent.children.create(name=c) 

     with self.assertRaises(AssertionError): # can't negative index 
      parents = Parent.objects.prefetch_related('children') 
      for parent in parents: 
       first = parent.children.all()[0] 
       last = parent.children.all()[-1] 

     with self.assertNumQueries(22): # 2 for prefetch and 20 for access 
      parents = Parent.objects.prefetch_related('children') 
      for parent in parents: 
       first = parent.children.first() 
       last = parent.children.last() 

     with self.assertNumQueries(22): # 2 for prefetch and 20 for access 
      parents = list(Parent.objects.prefetch_related('children')) 
      for parent in parents: 
       first = parent.children.first() 
       last = parent.children.last() 

     with self.assertNumQueries(12): # 2 for prefetch and 10 for last 
      parents = Parent.objects.prefetch_related('children') 
      for parent in parents: 
       first = parent.children.all()[0] 
       last = parent.children.reverse()[0] 

     with self.assertRaises(AssertionError): # can't negative index 
      parents = list(Parent.objects.prefetch_related('children')) 
      for parent in parents: 
       first = parent.children.all()[0] 
       last = parent.children.all()[-1] 

     with self.assertNumQueries(2): # 2 for prefetch 
      parents = Parent.objects.prefetch_related('children') 
      for parent in parents: 
       children = list(parent.children.all()) 
       first = children[0] 
       last = children[-1] 

     with self.assertNumQueries(2): 
      parents = Parent.objects.prefetch_related('children') 
      for parent in parents: 
       first = parent.children.all()[0] 
       last = parent.children.all()[parent.children.count() - 1] 
相關問題