2014-10-22 19 views
23

當我爲我的任何模型字段更改help_textverbose_name並運行python manage.py makemigrations時,它會檢測這些更改並創建新的遷移,例如0002_xxxx.py爲什麼Django會針對help_text和verbose_name進行遷移?

我正在使用PostgreSQL,我認爲這些更改與我的數據庫無關(我想知道是否存在這些更改相關的DBMS)。

爲什麼Django會爲這些更改生成遷移?是否可以忽略它們?

我可以申請從0002_xxxx.py以前的遷移(0001_initial.py)手動和安全的變化刪除0002_xxxx.py

有沒有辦法自動更新以前的遷移?

+2

從andrewgodwin此評論回答了這個問題,但部分我仍然希望能夠更改'help_text'而無需更新遷移:https://code.djangoproject.com/ticket/21498#comment:6 – utapyngo 2014-10-22 09:55:26

回答

7

This ticket解決了這個問題。

如果您僅更改了help_text & django生成新的遷移;那麼您可以將最新遷移的更改應用到先前的遷移,並刪除最新的遷移。

只需將以前遷移中的help_text更改爲最新遷移中的help_text並刪除最新的遷移文件即可。如果存在,請確保刪除相應的*.pyc文件。否則會引發異常。

4

爲了避免不必要的遷移可以執行如下操作:使遷移

  • 編寫定製解構該領域內的方法
  • 利潤
    1. 子類字段

    實施例:

    from django.db import models 
    
    class CustomCharField(models.CharField): # or any other field 
    
        def deconstruct(self): 
         name, path, args, kwargs = super(CustomCharField, self).deconstruct() 
         # exclude all fields you dont want to cause migration, my example below: 
         if 'help_text' in kwargs: 
          del kwargs['help_text'] 
         if 'verbose_name' in kwargs: 
          del kwargs['verbose_name'] 
         return name, path, args, kwargs 
    

    希望可以幫到

    1

    正如@ChillarAnand指出的,有一張票可以解決這個問題,但是直到現在(django 1.9.1)migrations命令還沒有修復。

    修復它的最小侵入性的方式是建立在<your-project>/management/commands/maketranslatedmigrations.py自己maketranslatedmigrations命令

    #coding: utf-8 
    
    from django.core.management.base import BaseCommand 
    from django.core.management.commands.makemigrations import Command as MakeMigrations 
    
    
    class Command(MakeMigrations): 
        leave_locale_alone = True 
        can_import_settings = True 
    
        def handle(self, *app_labels, **options): 
         super(Command, self).handle(*app_labels, **options) 
    

    然後你就可以用它完全一樣,原來makemigrations。

    P.S.不要忘記到處添加路徑

    +0

    最初的問題是關於'makemigrations',而不是'maketranslations'。 – utapyngo 2016-01-11 12:15:19

    +0

    謝謝你指出我的錯字 – katomaso 2016-01-12 16:41:28

    0

    我已經寫了自定義模塊用於這一目的

    所有你需要的是將其保存在一些utils的/模型上__init__.py文件。db和您的所有車型,而不是from django.db import modelsfrom utils import models

    如果有人有興趣的話,我可以寫一個組件併發布PyPI上

    UPD:試試這個https://github.com/FeroxTL/django-migration-control

    # models.py

    # -*- coding: utf-8 -*- 
    from types import FunctionType 
    from django.db import models 
    
    
    class NoMigrateMixin(object): 
        """ 
        Позволяет исключить из миграций различные поля 
        """ 
        def deconstruct(self): 
         name, path, args, kwargs = super(NoMigrateMixin, self).deconstruct() 
         kwargs.pop('help_text', None) 
         kwargs.pop('verbose_name', None) 
         return name, path, args, kwargs 
    
    
    # ============================================================================= 
    # DJANGO CLASSES 
    # ============================================================================= 
    
    for name, cls in models.__dict__.items(): 
        if isinstance(cls, type): 
         if issubclass(cls, models.Field): 
          # Поля 
          globals()[name] = type(name, (NoMigrateMixin, cls), {}) 
         else: 
          # Всякие менеджеры 
          globals()[name] = cls 
        elif isinstance(cls, FunctionType): 
         # Прочие функции 
         globals()[name] = cls 
    
    8

    你可以squash it with the previous migration,當然。

    或者,如果你不想在輸出所有的遷移,你可以把這個在management/commands/makemigrations.py在你的應用覆蓋makemigrationsmigrate命令:

    from django.core.management.commands.makemigrations import Command 
    from django.db import models 
    
    IGNORED_ATTRS = ['verbose_name', 'help_text', 'choices'] 
    
    original_deconstruct = models.Field.deconstruct 
    
    def new_deconstruct(self): 
        name, path, args, kwargs = original_deconstruct(self) 
        for attr in IGNORED_ATTRS: 
        kwargs.pop(attr, None) 
        return name, path, args, kwargs 
    
    models.Field.deconstruct = new_deconstruct 
    
    +0

    謝謝你指出這一點!在事實發生後似乎很明顯。對於一些人來說這是一個哲學的東西(完整的遷移);這在理論上很好,除非它在實踐中不起作用。特別是在help_text,選項和「存儲」!我們的存儲管理器和路徑是在運行時確定的,所以......您希望我爲遷移製作一個虛擬存儲?嘎。這解決了它......將模型問題與運行時問題分開,因爲它應該是。 – 2017-06-23 12:45:00

    相關問題