Opened 8 years ago

Last modified 8 years ago

#27081 closed Bug

DateField with current date intitialisation pypy migration issue — at Version 1

Reported by: Sergey Kurdakov Owned by: nobody
Component: Migrations Version: 1.9
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Sergey Kurdakov)

if model contains DateField and it is initialized with current date

example:

from datetime import date

class TestClass(models.Model):

    start_date = models.DateField(
        verbose_name=u'start date',
        default=date.today,
        )

pypy migrations will fail

django/db/migrations/writer.py", line 540, in serialize "topics/migrations/#migration-serializing" % (value, get_docs_version())
ValueError: Cannot serialize: <bound method type.today of <class 'datetime.date'>>

reason:
code for serialising methods in Django checks
if isinstance(value, (types.FunctionType, types.BuiltinFunctionType))
which succeeds on cpython because datetime.date.today is a
BuiltinFunctionType, wheras it's a types.MethodType on pypy and this check is missing in django
( link https://github.com/django/django/blob/3b383085fb89a48e756383e7cd5d3bd867353ba1/django/db/migrations/serializer.py#L379 )

a solution for client code is to declare local function

def today():
    return date.today()


class TestClass(models.Model):

    start_date = models.DateField(
        verbose_name=u'start date',
        default=today,
        )

but would be better to add types.MethodType check for pypy compatibility

Change History (1)

comment:1 by Sergey Kurdakov, 8 years ago

Description: modified (diff)
Note: See TracTickets for help on using tickets.
Back to Top