Tuesday, August 19, 2014

Re: Problems with time field in mysql

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


2014-08-19 17:31 GMT+02:00 Andreas Kuhne <andreas.kuhne@suitopia.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

2014-08-19 17:24 GMT+02:00 Sergiy Khohlov <skhohlov@gmail.com>:

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


On Tue, Aug 19, 2014 at 6:09 PM, Andreas Kuhne <andreas.kuhne@suitopia.com> wrote:
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

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@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.

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@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/CADTRxJOU%3DtBKpwBFw_Afd73HBzu3vF5V%3DZy8NbHouFG20O_f1Q%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@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/CALXYUbnUbr49Qk-SeBEUDHrSBdQngM7Wt4Xo4w8JOGD0%3DQ5rrg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment