0

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)

1 Answer 1

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. 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.

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. 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.

Sign up to request clarification or add additional context in comments.

4 Comments

Like I mentioned in my question , I did try taking out the str cast in my query. And also my flight_date is a DateField .
And like I mentioned in my answer, sqlite allows you to put anything into an field. You should post your full models. and also show what tommorow_date looks like
Still not MCVE please add the FlightSchedule model also
have added the FlightSchedule also

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.