[英]django.db.utils.ProgrammingError: operator does not exist: character varying date
I am querying from a PostGre db in my Django project. 我正在从Django项目中的PostGre数据库查询。
Below is my code. 下面是我的代码。
flight_schedule_detail_instance = FlightScheduleDetail.objects.filter
schedule_id=FlightSchedule.objects.filter(schedule_id=instance.schedule_id),
flight_date=str(tomorrow_date)
)
I need to filter all the instances which has a particular schedule_id
and a flight_date
. 我需要过滤所有具有特定schedule_id
和flight_date
的实例。 This works in my localhost. 这适用于我的本地主机。 I am using sqlite in my localhost code. 我在本地主机代码中使用sqlite。 The server uses PostGre db. 服务器使用PostGre db。 How do I query to get my desired results? 如何查询以获得所需的结果?
Also, flight_date
in my FlightScheduleDetail
model is a DateField
. 另外,我的FlightScheduleDetail
模型中的flight_date
是DateField
。 And my schedule_id
is a Foriegn key to another model which is a CharField
in that model. 我的schedule_id
是另一个模型的Foriegn键,该模型就是该模型中的CharField
。
I also tried without having a str
wrapper to my tomorrow_date
variable which is a date
object. 我还尝试了没有对tomorrow_date
的date
变量str
包装。
My models.py
: 我的models.py
:
class FlightScheduleDetail(models.Model):
flight_date = models.DateField(max_length=30, null=False, blank=False)
schedule_id = models.ForeignKey(FlightSchedule, null=False, related_name="schedule_details", blank=False)
route_id = models.CharField(max_length=150, null=False, blank=False, unique=True)
flight_status = models.ForeignKey(Status, null=True, default=1)
def __unicode__(self):
return u'%s' % self.route_id
class Meta:
verbose_name_plural = "flights Schedule Details"
class FlightSchedule(models.Model):
tail_number = models.ForeignKey(TailNumber, null=False, blank=False)
schedule_id = models.CharField(max_length=40, null=False, blank=False, unique=True)
flight_group_code = models.ForeignKey(FlightGroup, null=False, blank=False)
minLength = MinLengthValidator(limit_value=7)
binary = RegexValidator(r'^[0-1]*$', 'Only 0s and 1s are allowed.')
scheduled_days = models.CharField(max_length=7, validators=[binary, minLength], null=True, blank=True,
default="1111111")
origin_port_code = models.ForeignKey(Port, null=False, related_name="Origin", blank=False)
destination_port_code = models.ForeignKey(Port, null=False, related_name="Destination", blank=False)
flight_departure_time = models.TimeField()
start_date = models.DateField()
end_date = models.DateField()
def __unicode__(self):
return u'%s' % self.schedule_id
class Meta:
verbose_name_plural = "flights Schedule"
and I am calculating my tomorrow_date
as follows : 我正在计算我的tomorrow_date
,如下所示:
tomorrow_date = datetime.date.today() + datetime.timedelta(days=1)
This is a classic example of why you should use the same database in development and production. 这是一个为什么要在开发和生产中使用相同数据库的经典示例。 Sqlite does not have a concept of types. Sqlite没有类型的概念。 You can declare field types but they are not strictly followed. 您可以声明字段类型,但不严格遵循它们。 So you can easily insert text data into Date fields. 因此,您可以轻松地将文本数据插入“日期”字段。 Postgresql on the other hand is very strict about types. 另一方面,PostgreSQL对类型非常严格。
Updating my answer based on updates to the question. 根据问题的更新来更新我的答案。
I am not quite sure, if you need that elaborate query. 我不确定,是否需要该详细查询。 You have an instance of a FlightSchedule
and that instance will give you direct access to all the related FilightScheduleDetail
instances. 您有一个FlightSchedule
实例,该实例将使您直接访问所有相关的FilightScheduleDetail
实例。 you can just filter on it like this 你可以像这样过滤它
instance.flightscheduledetail_set.filter(flight_date=tommorow_date)
If you continue to have the error, it can only mean that there is a missig migration somewhere. 如果继续出现错误,则仅表示某处存在Missig迁移。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.