I
I
IvanOne2015-10-21 08:06:35
Django
IvanOne, 2015-10-21 08:06:35

How to make a selection in django?

Django 1.8 has two models like this

class PassRoute(BaseRoute):
    route_num = models.CharField(u"Номер маршрута", max_length=10, null=True, blank=True)
    sum = models.DecimalField(u"Сумма", max_digits=10, decimal_places=2)
    people = models.PositiveIntegerField(u"Колличество человек", null=True, blank=True)
    info = models.CharField(u"Дополнительная информация", max_length=200, null=True, blank=True)
    permanent = models.BooleanField(u"Ежедневный маршрут", default=True)

    def __unicode__(self):
        return u"{} - {}".format(self.start_point.city.name, self.end_point.city.name)


class DateRoute(models.Model):
    DAYS_WEEK = (
        (1, u"Понедельник"),
        (2, u"Вторник"),
        (3, u"Среда"),
        (4, u"Четверг"),
        (5, u"Пятница"),
        (6, u"Суббота"),
        (7, u"Воскресение")
    )
    MONTH_NUM = ([(x, x) for x in range(1, 32)])

    date_start = models.DateField(u"Дата отправления", null=True, blank=True)
    date_month = models.PositiveIntegerField(u"День месяца", null=True, blank=True, choices=MONTH_NUM)
    week_num = models.PositiveSmallIntegerField(u"День недели", choices=DAYS_WEEK, null=True, blank=True)

    route = models.ForeignKey(PassRoute, verbose_name=u"Маршрут")

The second model, as it were, creates a schedule for the first one, the question is how to choose routes for a specific day, that is, I take the 22nd, which is Thursday, respectively, I am looking for all schedules that fall on this day both by date and by day of the week or by number, and then select the appropriate routes from them, but the routes whose permanent field is set to True should also participate in the selection, I can’t figure out how to build a request through orm, what else would be the ability to sort? Or use bare sql? Postgresql db. Manipulations with the date have already been made, only the principle of constructing such a query is of interest.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Kitaev, 2015-10-21
@deliro

Q

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question