Thursday, March 18, 2021

Django 3.1 Makemigrations fails with FilePathField but succeeds with CharField

Converting path from Pathlib to str, allowed makemigrations to pass, at the cost of having a single test to rewrite

class Firmware(models.Model):
    filepath = models.FilePathField(path=str(settings.VENDOR_STATIC_ROOT), match=settings.FIRMWARE_REGEX, recursive=True)
    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))


I would be very curious to get a deeper understanding of what is happening here.
Thanks in advance.

Le jeudi 18 mars 2021 à 16:51:13 UTC+1, Olivier a écrit :
PS: In case the errors comes from the  '/var/www/vhosts/foo/vendorstatic' value itself , may I add that VENDOR_STATIC_ROOT is defined in settings.py, with:

from pathlib import Path
BASE_DIR = Path(__file__).resolve(strict=True).parent.parent
VENDOR_STATIC_ROOT = BASE_DIR / 'vendorstatic'

and that /var/www/vhosts/foo/vendorstatic directory exists.

Python version 3.7.3 and Django 3.1.7.
Le jeudi 18 mars 2021 à 15:28:21 UTC+1, Olivier a écrit :
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.

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)
    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 ?

3. How to work around this with rewriting my app (and its tests) ?


Best regards

--
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/c55fc16e-1289-4b49-ae12-6f3eff070caen%40googlegroups.com.

No comments:

Post a Comment