2015-10-15 9 views
5

के संदर्भ में कोड चल रहा है या नहीं, मेरे पास गतिशील विकल्पों वाला एक मॉडल है, और यदि मैं गारंटी दे सकता हूं कि कोड की स्थिति में कोड चल रहा है तो मैं एक खाली विकल्प सूची वापस करना चाहूंगा django-admin.py migrate/makemigrations आदेश या तो बेकार पसंद परिवर्तनों के बारे में या तो चेतावनी को रोकने के लिए आदेश।पता लगाएँ कि माइग्रेट/makemigrations कमांड

कोड:

from artist.models import Performance 
from location.models import Location 

def lazy_discover_foreign_id_choices(): 
    choices = [] 

    performances = Performance.objects.all() 
    choices += {performance.id: str(performance) for performance in performances}.items() 

    locations = Location.objects.all() 
    choices += {location.id: str(location) for location in locations}.items() 

    return choices 
lazy_discover_foreign_id_choices = lazy(lazy_discover_foreign_id_choices, list) 


class DiscoverEntry(Model): 
    foreign_id = models.PositiveIntegerField('Foreign Reference', choices=lazy_discover_foreign_id_choices(),) 

तो मैं अगर मैं lazy_discover_foreign_id_choices में रन संदर्भ पता लगा सकते हैं तो मैं उत्पादन के लिए एक खाली विकल्प सूची चुन सकते हैं लगता होगा। मैं sys.argv और __main__.__name__ का परीक्षण करने के बारे में सोच रहा था, लेकिन मुझे उम्मीद है कि संभवतः एक और अधिक विश्वसनीय तरीका या एपीआई है?

+1

आपकी पसंद कितनी गतिशील हैं? क्या आप कुछ कोड पोस्ट कर सकते हैं? – aumo

+0

निश्चित बात, कोड जोड़ा गया – DanH

+0

आप 'प्रदर्शन' और 'स्थान' कैसे आयात करते हैं? – Ivan

उत्तर

2

एक समाधान जो मैं सोच सकता हूं, वास्तव में वास्तविक ऑपरेशन करने से पहले झंडा सेट करने के लिए Django makemigrations कमांड को उपclass करना होगा।

उदाहरण:

, <someapp>/management/commands/makemigrations.py में उस कोड रखो यह Django के डिफ़ॉल्ट makemigrations आदेश पर आ जाएगी।

from django.core.management.commands import makemigrations 
from django.db import migrations 


class Command(makemigrations.Command): 
    def handle(self, *args, **kwargs): 
     # Set the flag. 
     migrations.MIGRATION_OPERATION_IN_PROGRESS = True 

     # Execute the normal behaviour. 
     super(Command, self).handle(*args, **kwargs) 

migrate कमांड के लिए ऐसा ही करें।

और आपके गतिशील विकल्पों समारोह को संशोधित:

from django.db import migrations 


def lazy_discover_foreign_id_choices(): 
    if getattr(migrations, 'MIGRATION_OPERATION_IN_PROGRESS', False): 
     return [] 
    # Leave the rest as is. 

यह बहुत hacky लेकिन काफी सेटअप करने के लिए आसान है।

import sys 
def lazy_discover_foreign_id_choices(): 
    if ('makemigrations' in sys.argv or 'migrate' in sys.argv): 
     return [] 
    # Leave the rest as is. 

यह सभी मामलों के लिए काम करना चाहिए:

+0

सुझाव के लिए धन्यवाद, लेकिन यह काम नहीं करता है। ऐसा लगता है कि मॉडल फ़ील्ड विकल्प 'Command.handle() ' – DanH

+0

से पहले निष्पादित होते हैं, मैंने इसे अपनी परियोजना पर करने की कोशिश की और यह काम करने लग रहा था, मैं जांच करूंगा। – aumo

+0

@DanH मैं पुष्टि करता हूं कि यह Django 1.8.5 के क्लीन इंस्टॉल पर ठीक काम करता है, क्या आप सुनिश्चित हैं कि यह नया आदेश है जो निष्पादित हो जाता है और डिफ़ॉल्ट नहीं है? – aumo

4

यहाँ यह करने के लिए (Django पहले से ही हमारे लिए झंडे बनाता है के बाद से) एक काफी गैर hacky तरीका है।

+0

ओह, यह करने के लिए यह वास्तव में एक अच्छा तरीका है। –

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