Problems with time field in mysql

48 views
Skip to first unread message

Andreas Kuhne

unread,
Aug 19, 2014, 3:10:15 PM8/19/14
to django...@googlegroups.com
Hi all,

I am having a problem with a time field in my mysql database. I can add values to the database, but when I try to read the values, I get the following error:
Failed converting row to Python types; can't use a string pattern on a bytes-like object (field start_time)

The database model looks like this:
class AvailableTimeSetting(models.Model):
    SETTINGS_CHOICES = (
        (0, _('Monday')),
        (1, _('Tuesday')),
        (2, _('Wednesday')),
        (3, _('Thursday')),
        (4, _('Friday')),
        (5, _('Saturday')),
        (6, _('Sunday')),
    )
    store = models.ForeignKey('store.RetailStore')
    weekday = models.IntegerField(_('Week day'),
                                  max_length=1,
                                  choices=SETTINGS_CHOICES)
    start_time = models.TimeField(_('Start time'))
    end_time = models.TimeField(_('End time'))
    max_parallel_bookings = models.IntegerField()

    class Meta:
        app_label = 'timebooking'
        verbose_name = 'Available time setting'
        verbose_name_plural = 'Available time settings'

    def __repr__(self):
        return "<%s: %s>" % (self.__class__.__name__, self.__str__())

    def __str__(self):
        return "%s %s-%s %d" % (self.weekday, self.start_time, self.end_time, self.max_parallel_bookings)


And the problem is on the start_time field. I see that the fields get saved correctly in the database. However as soon as I try to get an object I get the error.

I am using mysql-connector-python version 1.2.2, and Python 3.4 in a django 1.6.4 project.

Regards,

Andréas

Sergiy Khohlov

unread,
Aug 19, 2014, 3:25:09 PM8/19/14
to django-users
which one error ?

 Look like error is related to  __str__ function  but I'm not sure (I'm using python 2.7 )

Many thanks,

Serge


+380 636150445
skype: skhohlov


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CALXYUb%3DAk1GfO5bknfsMJ7cCYdFC0JACc3TXKb1cbFQHpFzppw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Andreas Kuhne

unread,
Aug 19, 2014, 3:31:42 PM8/19/14
to django...@googlegroups.com
Thanks Sergly,

However, the problem is already when I try and get the object from the database. I know that it is the conversion from mysql to python that is the problem.

This is the error message I get:
Failed converting row to Python types; can't use a string pattern on a bytes-like object (field start_time)

And it appears in mysql/connector/cursor.py in _row_to_python on row 394.

Regards,

Andréas

Andreas Kuhne

unread,
Aug 20, 2014, 6:03:27 AM8/20/14
to django...@googlegroups.com
I traced the problem down to one row in the mysql-connector-python lib code. When they try to read a time from the database, they do it with the following:

"""Custom converter for Django"""
    def _TIME_to_python(self, value, dsc=None):
        """Return MySQL TIME data type as datetime.time()

        Returns datetime.time()
        """
        return dateparse.parse_time(value)

I Changed that to:

"""Custom converter for Django"""
    def _TIME_to_python(self, value, dsc=None):
        """Return MySQL TIME data type as datetime.time()

        Returns datetime.time()
        """
        try:
            return dateparse.parse_time(value.decode())
        except:
            return dateparse.parse_time(value)


because the incoming value is a bytestring, which dateparse.parse couldn't read. When I used decode on it everything works perfectly.

Regards,

Andréas
Reply all
Reply to author
Forward
0 new messages