简体   繁体   English

Django将MySQL查询翻译成Django查询

[英]Django translating MySQL query to Django Query

How can i get the equivalent of this MySQL script in Django views? 我如何在Django视图中获得与该MySQL脚本等效的内容? The depart_city, arrive_city, and travel_date will be inputed by the user. 用户将输入depart_city,arrival_city和travel_date。

Here is my Models 这是我的模特儿

class Driver(models.Model):
first_name = models.CharField(max_length=30, null=True, blank=False)
last_name = models.CharField(max_length=30, null=True, blank=False)




class Schedule(models.Model):
depart_time = models.TimeField()
arrive_time = models.TimeField()



class TravelDate(models.Model):
start_date = models.DateField(null = True)
interval = models.IntegerField(null = True) 


class Route(models.Model): 
depart_city = models.CharField(max_length=50, null=True, blank=False)
arrive_city = models.CharField(max_length=50, null=True, blank=False)
driver = models.ForeignKey(Driver)
schedule = models.ForeignKey(Schedule)
traveldate = models.ForeignKey(TravelDate)

Here is my MySQL script. 这是我的MySQL脚本。 This works when i run it on MySQL workbench but I'm not sure how to translate this to Django Query 当我在MySQL工作台上运行它时此方法有效,但我不确定如何将其转换为Django Query

    SELECT busapp_route.depart_city, busapp_route.arrive_city, busapp_driver.first_name, busapp_schedule.depart_time 
FROM (((busapp_route INNER JOIN busapp_driver ON busapp_route.driver_id = busapp_driver.id) 
INNER JOIN busapp_schedule ON busapp_route.schedule_id = busapp_schedule.id)
INNER JOIN busapp_traveldate ON busapp_route.traveldate_id = busapp_traveldate.id) 
WHERE busapp_route.depart_city='Tropoje' AND busapp_route.arrive_city='Tirane'
AND (DATEDIFF('2017-11-26', busapp_traveldate.start_date) % busapp_traveldate.interval = 0);

Django doesn't support MySQL DATEDIFF natively. Django本机不支持MySQL DATEDIFF。 As a workaround, you could use something like this: 作为一种解决方法,您可以使用以下方法:

from django.db.models.expressions import RawSQL

routes = Route.objects\
    .values('depart_city', 'arrive_city', 'driver__first_name', 'schedule__depart_time', 'traveldate__start_date')\
    .annotate(datediff_mod=RawSQL("DATEDIFF(%s, busapp_traveldate.start_date) MOD busapp_traveldate.interval", ('2017-11-26', )))\
    .filter(depart_city='Tropoje', arrive_city='Tirane', datediff_mod = 0)

I don't use MySQL so I couldn't test this, but I'm pretty sure it should work or maybe at least give you an idea how to implement it. 我不使用MySQL,所以无法测试它,但是我很确定它应该可以工作,或者至少可以给您一个实现它的想法。

I don't think that it's a good idea, trying to solve this from the SQL side. 我认为尝试从SQL端解决此问题不是一个好主意。

And what you're building there, is not trivial as there are a lot of things to keep in mind. 而且您在此处构建的内容并不是一件容易的事,因为有很多事情需要牢记。

Your current Model setup lags some points: What if a user searches for a travel in next year? 您当前的“模型”设置存在一些不足:如果用户明年搜索旅行怎么办? With your model you'll have to create entries for all possible dates in the future, that's nearly unusable. 使用模型,您将不得不为将来的所有可能日期创建条目,这几乎是不可用的。

Think also about separating times and dates in different models, as they usually should be kept together, cause a ride from A to B at date x and time y is one object. 认为还有关在不同的模式中分离的时间和日期,因为它们通常应保持在一起,使从A乘坐在日期x和时间Y B是一个对象。

It might help to have a look at django-scheduler , they solved some troubles with the use of Events and Occurences. 看看django-scheduler可能会有所帮助,他们使用事件和事件解决了一些麻烦。 Have a look at their pretty good docs. 看看他们相当不错的文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM