2014-10-22 4 views
23

जब मैं अपने किसी भी मॉडल फ़ील्ड के लिए help_text या verbose_name बदलता हूं और python manage.py makemigrations चलाता हूं, तो यह इन परिवर्तनों का पता लगाता है और 0002_xxxx.py कहता है, एक नया माइग्रेशन बनाता है।Django help_text और verbose_name परिवर्तनों के लिए माइग्रेशन क्यों करता है?

मैं PostgreSQL उपयोग कर रहा हूँ और मुझे लगता है कि इन परिवर्तनों मेरी डेटाबेस के लिए अप्रासंगिक हैं (मुझे आश्चर्य है अगर एक डीबीएमएस जिसके लिए इन परिवर्तनों को प्रासंगिक हैं सब पर मौजूद है)।

Django ऐसे परिवर्तनों के लिए माइग्रेशन क्यों उत्पन्न करता है? क्या यह उन्हें अनदेखा करने का विकल्प है?

क्या मैं 0002_xxxx.py से पिछले माइग्रेशन (0001_initial.py) में परिवर्तनों को मैन्युअल रूप से और सुरक्षित रूप से 0002_xxxx.py हटा सकता हूं?

क्या पिछले माइग्रेशन को स्वचालित रूप से अपडेट करने का कोई तरीका है?

+2

andrewgodwin से यह टिप्पणी सवाल का जवाब आंशिक रूप से लेकिन मैं अभी भी माइग्रेशन अपडेट करने के बिना 'help_text' को बदलने में सक्षम होना चाहता हूं: https://code.djangoproject.com/ticket/21498#comment:6 – utapyngo

उत्तर

7

This ticket समस्या को संबोधित किया।

यदि आपने केवल help_text & बदल दिया है django एक नया माइग्रेशन उत्पन्न करता है; तो आप नवीनतम माइग्रेशन से पिछले माइग्रेशन में परिवर्तन लागू कर सकते हैं और नवीनतम माइग्रेशन को हटा सकते हैं।

बस नवीनतम प्रवास में वर्तमान help_text और नवीनतम माइग्रेशन फ़ाइल को नष्ट करने के लिए पिछले माइग्रेशन में help_text बदल जाते हैं। यदि मौजूद है तो संबंधित *.pyc फ़ाइल को निकालना सुनिश्चित करें। अन्यथा एक अपवाद उठाया जाएगा।

4

अनावश्यक माइग्रेशन आप के रूप में कर सकते हैं से बचने के लिए इस प्रकार है:

  1. उपवर्ग क्षेत्र का कारण बनता है कि माइग्रेशन
  2. कि क्षेत्र के अंदर विधि deconstruct लिखें कस्टम
  3. लाभ

उदाहरण:

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) माइग्रेशन कमांड तय नहीं किए गए थे।

यह तय करने में कम से कम दखल तरीके के रूप में

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

<your-project>/management/commands/maketranslatedmigrations.py में अपने स्वयं के maketranslatedmigrations आदेश बनाने के लिए है और फिर आप यह वास्तव में मूल makemigrations के रूप में ही उपयोग कर सकते हैं।

पीएस पथ

+0

मूल प्रश्न' makemrrations 'के बारे में था,' maketranslations 'नहीं। – utapyngo

+0

मेरे टाइपो – katomaso

0

मैं उस उद्देश्य

आपको बस कुछ utils/मॉडल में सहेजने के लिए है के लिए कस्टम मॉड्यूल लिखा है पर हर जगह __init__.py फ़ाइलें जोड़ने के लिए मत भूलना।डाटाबेस और from django.db import models लिखने के बजाय अपने सभी मॉडलों में from utils import models

अगर किसी को यह में रुचि रखता है मैं एक घटक लिखने और pypi पर प्रकाशित कर सकते हैं

युपीडी: कोशिश इस 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 में इस डालकर makemigrations और migrate आदेश ओवरराइड कर सकते हैं:

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, विकल्प, और 'भंडारण' के साथ! हमारा स्टोरेज मैनेजर और पथ रनटाइम पर निर्धारित किया जाता है ... क्या, आप चाहते हैं कि मैं माइग्रेशन के लिए नकली भंडारण करूं? गाह। यह हल करता है ... मॉडल चिंताओं से अलग रनटाइम चिंताओं, जैसा कि यह होना चाहिए। –

संबंधित मुद्दे