Sunday, June 27, 2021

Re: Django 3.1 Makemigrations fails with FilePathField but succeeds with CharField

Hi Peter, I hope I can help.

On Wed, Jun 23, 2021 at 1:50 PM Peter of the Norse <rahmcoff@gmail.com> wrote:
I think you might be better off using https://docs.djangoproject.com/en/3.2/topics/migrations/#squashing-migrations than having it recreate all of them.  

On Mar 18, 2021, at 8:28 AM, Olivier <oza.4h07@gmail.com> wrote:

Hello,

I've got a Django app which has over 200 migration steps.
To shorten its test loading time, I'm trying to erase all current migrations and restart from scratch.

Yep, pretty reasonably if you have the luxury of re-creating/reloading your db.  If you don't, you can squash your migrations, even if it leaves the original migration files intact until you manually remove them (when it is safe to do so).

Experience suggests that it isn't migrations that accounts for a big share of test startup time - it's the test discovery which takes so long...
 
I deteted and re-created my app Postgres database.
I deleted all files (but __init__.py) in migrations directory.
I typed "python manage.py makemigrations --verbosity 3".

1. Latest makemigrations failed with:
  File "/home/perenip/venv-flexcore/lib/python3.7/site-packages/django/db/migrations/serializer.py", line 339, in serializer_factory
    "topics/migrations/#migration-serializing" % (value, get_docs_version())
ValueError: Cannot serialize: PosixPath('/var/www/vhosts/foo/vendorstatic')
There are some values Django cannot serialize into migration files.
For more, see https://docs.djangoproject.com/en/3.1/topics/migrations/#migration-serializing

If I'm not mistaken, I couldn't read any reference to the Model/Field that triggered the error.
Should we expect an explicit mention of the faulty statement/line ?

2. I edited a Model within models.py with:
class Firmware(models.Model):
    #filepath = models.FilePathField(path=settings.VENDOR_STATIC_ROOT, match=settings.FIRMWARE_REGEX, recursive=True)

This is the issue. Your settings for VENDOR_STATIC_ROOT is a Path, not a string.

So in your settings use VENDOR_STATIC_ROOT = str(whatever expression you initialise it with)

Should fix this immediately.
 
    filepath = models.CharField(max_length=64)
    display_name = models.CharField(default='foobar', max_length=32, help_text='Value as shown in HTTP User-Agent string')

    def __str__(self):
        return self.relative_path()

    def relative_path(self):
        return str(pathlib.Path(self.filepath).relative_to(settings.VENDOR_STATIC_ROOT))

After changing from a PathFileField to a CharFiled as shown above, I could successfully run makemigrations.

Doc [1] mentions Django can serialize "any Django field". Should it serialize FilePathField ?


Yeah it's not the field at fault, it is the path= initialiser :-)
 
3. How to work around this with rewriting my app (and its tests) ?


HTH,
David 

--
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 view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAE5VhgUG0p4vRu3KLN1%3DkgVMZ%3DnUWmWHYA35eJxYBQYwpP0XCA%40mail.gmail.com.

No comments:

Post a Comment