Re: [pypy-dev] Django DateField default value and migrations fail
Hi William, here is my traceback in console Traceback (most recent call last): File "manage.py", line 10, in <module> execute_from_command_line(sys.argv) File "/usr/local/lib/pypy2.7/dist-packages/django/core/management/__init__.py", line 353, in execute_from_command_line utility.execute() File "/usr/local/lib/pypy2.7/dist-packages/django/core/management/__init__.py", line 345, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "/usr/local/lib/pypy2.7/dist-packages/django/core/management/base.py", line 348, in run_from_argv self.execute(*args, **cmd_options) File "/usr/local/lib/pypy2.7/dist-packages/django/core/management/base.py", line 399, in execute output = self.handle(*args, **options) File "/usr/local/lib/pypy2.7/dist-packages/django/core/ management/commands/makemigrations.py", line 150, in handle self.write_migration_files(changes) File "/usr/local/lib/pypy2.7/dist-packages/django/core/ management/commands/makemigrations.py", line 178, in write_migration_files migration_string = writer.as_string() File "/usr/local/lib/pypy2.7/dist-packages/django/db/migrations/writer.py", line 167, in as_string operation_string, operation_imports = OperationWriter(operation). serialize() File "/usr/local/lib/pypy2.7/dist-packages/django/db/migrations/writer.py", line 124, in serialize _write(arg_name, arg_value) File "/usr/local/lib/pypy2.7/dist-packages/django/db/migrations/writer.py", line 76, in _write arg_string, arg_imports = MigrationWriter.serialize(item) File "/usr/local/lib/pypy2.7/dist-packages/django/db/migrations/writer.py", line 357, in serialize item_string, item_imports = cls.serialize(item) File "/usr/local/lib/pypy2.7/dist-packages/django/db/migrations/writer.py", line 433, in serialize return cls.serialize_deconstructed(path, args, kwargs) File "/usr/local/lib/pypy2.7/dist-packages/django/db/migrations/writer.py", line 318, in serialize_deconstructed arg_string, arg_imports = cls.serialize(arg) File "/usr/local/lib/pypy2.7/dist-packages/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'>> Regards Sergey On Wed, Aug 17, 2016 at 10:21 AM, William ML Leslie < william.leslie.ttg@gmail.com> wrote:
On 17 August 2016 at 16:50, Sergey Kurdakov <sergey.forum@gmail.com> wrote:
still causes error of the kind:
topics/migrations/#migration-serializing" % (value, get_docs_version()) ValueError: Cannot serialize: <bound method type.today of <class 'datetime.date'
Do you get a full traceback? That would be much more useful for tracking down the problem.
-- William Leslie
Notice: Likely much of this email is, by the nature of copyright, covered under copyright law. You absolutely MAY reproduce any part of it in accordance with the copyright law of the nation you are reading this in. Any attempt to DENY YOU THOSE RIGHTS would be illegal without prior contractual agreement.
Hi William. I have created a test project ( just a new Django Project in PythonStudio so it is tested in Windows too, but this code will work on linux too ) http://ananas.ws/media/DjangoTest.zip unzip go to DjangoTest main directory and try to make migrations: DjangoTest>pypy manage.py makemigrations you'll get an error at the only model in the app DjangoTest\app\models.py from datetime import date class TestClass(models.Model): start_date = models.DateField( verbose_name=u'start date', default=date.today, ) Best regards Sergey
I took a look at the Django source. There's code for serialising methods in Django that 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. Maybe that check could be expanded to include MethodType. Here's the code at master: https://github.com/django/django/blob/3b383085fb89a48e756383e7cd5d3bd867353b... On 17 August 2016 at 17:27, Sergey Kurdakov <sergey.forum@gmail.com> wrote:
Hi William,
here is my traceback in console
Traceback (most recent call last): File "manage.py", line 10, in <module> execute_from_command_line(sys.argv) File "/usr/local/lib/pypy2.7/dist-packages/django/core/management/__init__.py", line 353, in execute_from_command_line utility.execute() File "/usr/local/lib/pypy2.7/dist-packages/django/core/management/__init__.py", line 345, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "/usr/local/lib/pypy2.7/dist-packages/django/core/management/base.py", line 348, in run_from_argv self.execute(*args, **cmd_options) File "/usr/local/lib/pypy2.7/dist-packages/django/core/management/base.py", line 399, in execute output = self.handle(*args, **options) File "/usr/local/lib/pypy2.7/dist-packages/django/core/management/commands/makemigrations.py", line 150, in handle self.write_migration_files(changes) File "/usr/local/lib/pypy2.7/dist-packages/django/core/management/commands/makemigrations.py", line 178, in write_migration_files migration_string = writer.as_string() File "/usr/local/lib/pypy2.7/dist-packages/django/db/migrations/writer.py", line 167, in as_string operation_string, operation_imports = OperationWriter(operation).serialize() File "/usr/local/lib/pypy2.7/dist-packages/django/db/migrations/writer.py", line 124, in serialize _write(arg_name, arg_value) File "/usr/local/lib/pypy2.7/dist-packages/django/db/migrations/writer.py", line 76, in _write arg_string, arg_imports = MigrationWriter.serialize(item) File "/usr/local/lib/pypy2.7/dist-packages/django/db/migrations/writer.py", line 357, in serialize item_string, item_imports = cls.serialize(item) File "/usr/local/lib/pypy2.7/dist-packages/django/db/migrations/writer.py", line 433, in serialize return cls.serialize_deconstructed(path, args, kwargs) File "/usr/local/lib/pypy2.7/dist-packages/django/db/migrations/writer.py", line 318, in serialize_deconstructed arg_string, arg_imports = cls.serialize(arg) File "/usr/local/lib/pypy2.7/dist-packages/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'>>
Regards Sergey
On Wed, Aug 17, 2016 at 10:21 AM, William ML Leslie <william.leslie.ttg@gmail.com> wrote:
On 17 August 2016 at 16:50, Sergey Kurdakov <sergey.forum@gmail.com> wrote:
still causes error of the kind:
topics/migrations/#migration-serializing" % (value, get_docs_version()) ValueError: Cannot serialize: <bound method type.today of <class 'datetime.date'
Do you get a full traceback? That would be much more useful for tracking down the problem.
-- William Leslie
Notice: Likely much of this email is, by the nature of copyright, covered under copyright law. You absolutely MAY reproduce any part of it in accordance with the copyright law of the nation you are reading this in. Any attempt to DENY YOU THOSE RIGHTS would be illegal without prior contractual agreement.
-- William Leslie Notice: Likely much of this email is, by the nature of copyright, covered under copyright law. You absolutely MAY reproduce any part of it in accordance with the copyright law of the nation you are reading this in. Any attempt to DENY YOU THOSE RIGHTS would be illegal without prior contractual agreement.
Hi William, thanks, I solved then my problem by following code def today(): """ Returns an aware or naive datetime.datetime, depending on settings.USE_TZ. """ return date.today() class TestClass(models.Model): start_date = models.DateField( verbose_name=u'start date', default=today, ) so I just wrap required function call into function. But this is a temporary solution. Would be great if there could be a general one. Regards Sergey On Wed, Aug 17, 2016 at 11:04 AM, William ML Leslie < william.leslie.ttg@gmail.com> wrote:
I took a look at the Django source.
There's code for serialising methods in Django that 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. Maybe that check could be expanded to include MethodType.
Here's the code at master:
https://github.com/django/django/blob/3b383085fb89a48e756383e7cd5d3b d867353ba1/django/db/migrations/serializer.py#L379
Hi William,
here is my traceback in console
Traceback (most recent call last): File "manage.py", line 10, in <module> execute_from_command_line(sys.argv) File "/usr/local/lib/pypy2.7/dist-packages/django/core/ management/__init__.py", line 353, in execute_from_command_line utility.execute() File "/usr/local/lib/pypy2.7/dist-packages/django/core/ management/__init__.py", line 345, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "/usr/local/lib/pypy2.7/dist-packages/django/core/management/base.py",
348, in run_from_argv self.execute(*args, **cmd_options) File "/usr/local/lib/pypy2.7/dist-packages/django/core/management/base.py",
399, in execute output = self.handle(*args, **options) File "/usr/local/lib/pypy2.7/dist-packages/django/core/management/commands/ makemigrations.py", line 150, in handle self.write_migration_files(changes) File "/usr/local/lib/pypy2.7/dist-packages/django/core/management/commands/ makemigrations.py", line 178, in write_migration_files migration_string = writer.as_string() File "/usr/local/lib/pypy2.7/dist-packages/django/db/migrations/writer.py",
167, in as_string operation_string, operation_imports = OperationWriter(operation).serialize() File "/usr/local/lib/pypy2.7/dist-packages/django/db/migrations/writer.py",
124, in serialize _write(arg_name, arg_value) File "/usr/local/lib/pypy2.7/dist-packages/django/db/migrations/writer.py",
76, in _write arg_string, arg_imports = MigrationWriter.serialize(item) File "/usr/local/lib/pypy2.7/dist-packages/django/db/migrations/writer.py",
357, in serialize item_string, item_imports = cls.serialize(item) File "/usr/local/lib/pypy2.7/dist-packages/django/db/migrations/writer.py",
433, in serialize return cls.serialize_deconstructed(path, args, kwargs) File "/usr/local/lib/pypy2.7/dist-packages/django/db/migrations/writer.py",
318, in serialize_deconstructed arg_string, arg_imports = cls.serialize(arg) File "/usr/local/lib/pypy2.7/dist-packages/django/db/migrations/writer.py",
On 17 August 2016 at 17:27, Sergey Kurdakov <sergey.forum@gmail.com> wrote: line line line line line line line line line
540, in serialize "topics/migrations/#migration-serializing" % (value, get_docs_version()) ValueError: Cannot serialize: <bound method type.today of <class 'datetime.date'>>
Regards Sergey
On Wed, Aug 17, 2016 at 10:21 AM, William ML Leslie <william.leslie.ttg@gmail.com> wrote:
On 17 August 2016 at 16:50, Sergey Kurdakov <sergey.forum@gmail.com> wrote:
still causes error of the kind:
topics/migrations/#migration-serializing" % (value,
get_docs_version())
ValueError: Cannot serialize: <bound method type.today of <class 'datetime.date'
Do you get a full traceback? That would be much more useful for tracking down the problem.
-- William Leslie
Notice: Likely much of this email is, by the nature of copyright, covered under copyright law. You absolutely MAY reproduce any part of it in accordance with the copyright law of the nation you are reading this in. Any attempt to DENY YOU THOSE RIGHTS would be illegal without prior contractual agreement.
-- William Leslie
Notice: Likely much of this email is, by the nature of copyright, covered under copyright law. You absolutely MAY reproduce any part of it in accordance with the copyright law of the nation you are reading this in. Any attempt to DENY YOU THOSE RIGHTS would be illegal without prior contractual agreement.
On Wed, 17 Aug 2016, Sergey Kurdakov wrote:
class TestClass(models.Model):
start_date = models.DateField( verbose_name=u'start date', default=today, )
so I just wrap required function call into function.
You can, of course, use a lambda, i.e. `default=lambda: date.today()` which is to the same effect, but a bit shorter.
But this is a temporary solution. Would be great if there could be a general one.
So does it work if you modify Django as William suggested? If yes, it sounds like this is better fixed in Django... -- Sincerely yours, Yury V. Zaytsev
Hi Yury, thanks for lambda tip, ok I will submit a ticket to Django, will see how it goes. Regards Sergey On Wed, Aug 17, 2016 at 12:53 PM, Yury V. Zaytsev <yury@shurup.com> wrote:
On Wed, 17 Aug 2016, Sergey Kurdakov wrote:
class TestClass(models.Model):
start_date = models.DateField( verbose_name=u'start date', default=today, )
so I just wrap required function call into function.
You can, of course, use a lambda, i.e. `default=lambda: date.today()` which is to the same effect, but a bit shorter.
But this is a temporary solution. Would be great if there could be a
general one.
So does it work if you modify Django as William suggested? If yes, it sounds like this is better fixed in Django...
-- Sincerely yours, Yury V. Zaytsev
On 17 August 2016 at 19:53, Yury V. Zaytsev <yury@shurup.com> wrote:
On Wed, 17 Aug 2016, Sergey Kurdakov wrote:
class TestClass(models.Model):
start_date = models.DateField( verbose_name=u'start date', default=today, )
so I just wrap required function call into function.
You can, of course, use a lambda, i.e. `default=lambda: date.today()` which is to the same effect, but a bit shorter.
It doesn't work with a lambda; the serialiser writes code that looks up the function by name. -- William Leslie Notice: Likely much of this email is, by the nature of copyright, covered under copyright law. You absolutely MAY reproduce any part of it in accordance with the copyright law of the nation you are reading this in. Any attempt to DENY YOU THOSE RIGHTS would be illegal without prior contractual agreement.
Hi William, On 17 August 2016 at 10:04, William ML Leslie <william.leslie.ttg@gmail.com> wrote:
There's code for serialising methods in Django that 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. Maybe that check could be expanded to include MethodType.
Yes, that's the right fix. The problem is that ``date.today`` returns a "built-in bound classmethod" object, which on CPython is identified as ``types.BuiltinFunctionType`` but on PyPy is identified as ``types.MethodType``. We can't add a workaround in PyPy, either: ``date.today`` cannot return a plain function, because ``Date.today`` is a class method, not a static method. A bientôt, Armin.
Hi All, after submitting a ticket a fix to Django is submitted by Django developer https://github.com/django/django/pull/7113 hope it will be in next Django release. Regards Sergey
participants (4)
-
Armin Rigo
-
Sergey Kurdakov
-
William ML Leslie
-
Yury V. Zaytsev