I am querying from a PostGre db in my Django project.
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 . This works in my localhost. I am using sqlite in my localhost code. The server uses PostGre db. How do I query to get my desired results?
Also, flight_date in my FlightScheduleDetail model is a DateField. And my schedule_id is a Foriegn key to another model which is a CharField in that model.
I also tried without having a str wrapper to my tomorrow_date variable which is a date object.
My 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 = datetime.date.today() + datetime.timedelta(days=1)