#27081: DateField with current date intitialisation pypy migration issue
-------------------------------+--------------------------------------
Reporter: Kurdakov | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 1.9
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by Kurdakov):
* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0
Old description:
> 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()
>
> but would be better to add types.MethodType check for pypy compatibility
> class TestClass(models.Model):
>
> start_date = models.DateField(
> verbose_name=u'start date',
> default=today,
> )
New description:
if model contains DateField and it is initialized with current date
example:
from datetime import date
{{{
#!python
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
{{{
#!python
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
--
--
Ticket URL: <https://code.djangoproject.com/ticket/27081#comment:1>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/066.7bbfcfec435a32bec74a19a7b23c5243%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.