Check in Django a model using ORM?

0
class Curso(models.Model):
    fecha =  models.DateField()
    hora_inicio = models.TimeField()
    hora_fin = models.TimeField()
    capacidad_maxima = models.PositiveSmallIntegerField(default=6)
    sede = models.ForeignKey(Sede)
    profesor =  models.ForeignKey(Profesor)
    estudiantes  =  models.ManyToManyField(Estudiante,blank=True)
    tipo_nivel  = models.CharField(max_length='2',default='xx')
    tipo_leccion = models.PositiveSmallIntegerField(default=0)
    max_tipo = models.PositiveSmallIntegerField(default=3)
    tipo_estudiante=models.ManyToManyField(Nivel,related_name='tipo_estudiante', blank=True)

I have this model, I need to make a Query that returns only the courses that are empty or at the student's level, additional if there are more courses at the same time and they are empty, just give me the various courses or the course of the student's level for that hour.

    
asked by David Pulloquinga 09.03.2016 в 22:53
source

2 answers

1

Man, it occurs to me that to know if there are courses at the same time you can use the following function:

def is_courses_in_same_hour(self):
    courses = Curso.objects.all()
    for course in courses:
        if course.model.hora_inicio == self.hora_inicio and course.model.hora_fin == self.hora_fin:
            return True

You can play with both filters, and create a function that returns the queryset you need.

    
answered by 10.03.2016 в 02:38
1

use 'from django.db.models import Q' to perform 'or' queries.

An example:

if form.cleaned_data['query']:
    query = form.cleaned_data['query']
    object_list = object_list.filter(
        Q(first_name__contains=query)
            | Q(last_name__contains=query)
            | Q(email__contains=query)
            | Q(identification_number__contains=query)
        ))
    
answered by 28.03.2016 в 23:56