Skip to content

Instantly share code, notes, and snippets.

@cansadadeserfeliz
Last active August 29, 2015 14:06
Show Gist options
  • Save cansadadeserfeliz/7eb46afbea3346a6c52b to your computer and use it in GitHub Desktop.
Save cansadadeserfeliz/7eb46afbea3346a6c52b to your computer and use it in GitHub Desktop.
Cosa loca de Django
invoice_6_contract_5_items = invoice_6.invoice_items.filter(
contract=self.contract5
)
print invoice_6_contract_5_items.first().id, invoice_6_contract_5_items.last().id
print invoice_6_contract_5_items[0].id, invoice_6_contract_5_items[1].id
output >>
22 22
22 23
In [1]: ii = InvoiceItem.objects.all()[:2]
In [2]: i = Invoice.objects.all()[:2]
In [3]: i.first().id, i.last().id
Out[3]: (23, 17)
In [4]: i.values_list('id', flat=True)
Out[4]: [23, 22]
In [5]: i.first().id, i.last().id
Out[5]: (23, 17)
In [6]: i
Out[6]: [<Invoice: 7>, <Invoice: 6>]
In [7]: i[0].id, i[1].id
Out[7]: (23, 22)
In [8]: Invoice.objects.all().last()
Out[8]: <Invoice: 1>
In [9]: Invoice.objects.all().last().id
Out[9]: 17
In [10]: type(Invoice.objects.all()[:2])
Out[10]: django.db.models.query.QuerySet
In [11]: type(Invoice.objects.all())
Out[11]: django.db.models.query.QuerySet
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment