Django port and reusable custom applications

I started writing my first reusable application about 3 weeks ago, and I had problems with migration.

I want some points of my application to be customized. Thus, I have a confsubodule that defines user settings and assigns reasonable defaults that will suit most cases.

This causes some of my model fields to look like this:

attachment = models.FilePathField(
    path=conf.ATTACHMENTS_DIR, recursive=True)

template_file = models.FileField(
    upload_to=conf.TEMPLATES_UPLOAD_DIR, blank=True)

prefix_subject = models.BooleanField(
    default=True, verbose_name=_("prefix subject"),
    help_text=_(
        "Whether to prefix the subject with \"{}\" or not."
    ).format(conf.SUBJECT_PREFIX))

Unfortunately, in projects using this application, this leads to the fact that it django-admin makemigrationscreates a migration for it every time the parameter changes. Or even the first time they install the application for settings, the default value depends on the host system.

, . , .

prefix_subject . , help_text .

, / . ?

, , , . - .

, :

migrations.CreateModel(
    name='MailStaticAttachment',
    fields=[
        ('id', ...),
        ('filename', ...)
        ('mime_type', ...)
        ('attachment', models.FilePathField(path='/home/antoine/Workspace/django-mailing/static/mailing/attachments', recursive=True, verbose_name='file')),
    ],
    options={...}
),

:

from ..conf import ATTACHMENTS_DIR

migrations.CreateModel(
    name='MailStaticAttachment',
    fields=[
        ('id', ...),
        ('filename', ...)
        ('mime_type', ...)
        ('attachment', models.FilePathField(path=ATTACHMENTS_DIR, recursive=True, verbose_name='file')),
    ],
    options={...}
),

?

?

, Field.help_text, FilePathField.path FileField.upload_to SQL. - " ". , , , Field.default, Field.db_column CharField.max_length ? , , , , , .: P , , -.

+4
1

django.db.migration , , .

upload_to , .

import os

def upload_to(instance, filename):
    return os.path.join([conf.TEMPLATES_UPLOAD_DIR, filename])

path help_text , , django-sundial, , , deconstruct(), .

from django.utils.six import python_2_unicode_compatible


@python_2_unicode_compatible
class StringConfReference(object):
    def __init__(self, name):
        self.name = name

    def __str__(self):
        return getattr(conf, self.name)

    def deconstruct(self):
        return "%s.%s" % (__name__, self.__class__.__name__), (self.name,), {}


attachment = models.FilePathField(
    path=StringConfReference('ATTACHMENT_DIR'), recursive=True
)
+2

Source: https://habr.com/ru/post/1629394/


All Articles