-
-
Save GeraldineE/3c64469b80875075650663ca6ff9b935 to your computer and use it in GitHub Desktop.
Ejemplo de consultas (Queryset) en Django 2017
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.db import models | |
# para todos los ejemplos de este gist, vamos a utilizar el modelo "Human" | |
class Human(models.Model): | |
'''Modelo de ejemplo que representa | |
un humano y sus atributos. | |
''' | |
first_name = models.CharField(max_length=30) | |
last_name = models.CharField(max_length=30) | |
birth_date = models.DateField() | |
Mother = models.ForeignKey('self', symmetrical=False) | |
def __str__(self): | |
return '{} {}: [{}]'.format( self.first_name, | |
self.last_name, | |
self.birth_date | |
) | |
''' vvv - CONSULTAS DE EJEMPLO - vvv ''' | |
# Consultar todos los objetos de un modelo | |
h = Human.objects.all | |
# Consultar un objeto de un modelo por ID (en este caso ID 1) | |
h = Human.objects.get(pk = 1) | |
h = Human.objects.get(id = 1) | |
# consultar todos los objetos que superen una fecha | |
# en este caso que hayan nacido despues del 01/01/2000 | |
start_date = datetime.date(2000, 1, 1) | |
end_date = datetime.date.today() | |
h = Human.objects.filter(birth_date__range=(start_date, end_date)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment