2009-12-28 12 views
8

चलें कहते हैं कि मैं दो Django क्षुधा है:Django: क्या ManyToManyField युक्त मॉडल के लिए एक अलग ऐप में ManyToManyField में "through" मॉडल रखने का कोई तरीका है?

  • प्रतियोगिताओं - जो प्रतियोगिता डेटा संभाल लेंगे
  • प्रविष्टियों - जो कार्यक्षमता प्रतियोगिताओं में प्रवेश प्रतियोगियों से संबंधित

में संभाल लेंगे प्रतियोगिताओं ऐप में मेरे पास एक मॉडल है जो प्रतिस्पर्धा के एक वर्ग का प्रतिनिधित्व करता है:

class Division(models.Model): 
    competition = models.ForeignKey(Competition) 
    discipline = models.CharField(max_length=1, choices=DISCIPLINE_CHOICES) 
    age_group = models.ForeignKey(AgeGroup) 
    participants = models.ManyToManyField(Competitor, through='Entry') 

मैं प्रविष्टियों अनुप्रयोग में एंट्री मॉडल रखना चाहता हूँ:

class Entry(models.Model): 
    division = models.ForeignKey('Division') 
    competitor = models.ForeignKey(Competitor) 
    withdrawn = models.BooleanField(default=False) 

मैं कैसे से ... आयात ... बयान को हल करते हैं, ताकि वे काम करते हैं?

Error: One or more models did not validate: entries.entry: 'division' has a relation with model Division, which has either not been installed or is abstract. competitions.division: 'participants' specifies an m2m relation through model Entry, which has not been installed

मुझे समझ में क्यों: जब मैं आयात बयान में डाल इस तरह के from entries.models import Entry के रूप में मैं जब मैं उनमें से एक या दोनों को दूर मैं सत्यापन त्रुटियों मिल मॉडल इन एप्लिकेशन को (क्योंकि आयात परिपत्र कर रहे हैं) या syncdb द्वारा नजरअंदाज कर दिया से मिलता है ऐसा होता है, लेकिन मुझे नहीं पता कि इसे कैसे बदला जाए, ताकि यह काम करे (एंट्री मॉडल को प्रतियोगिताओं ऐप में ले जाने के बिना, जो मैं वास्तव में नहीं करना चाहता)।

+0

आप पोस्ट कर सकते हैं 'प्रतियोगी 'वर्ग? – czarchaic

उत्तर

16

ऐसा लगता है Django documentation on the ForeignKey class कहते हैं: अपने उदाहरण में, यह करने के लिए Entry में सामान्य रूप से Entry आयात और division FK परिभाषा chage करने के लिए कोशिश :

To refer to models defined in another application, you can explicitly specify a model with the full application label. For example, if the Manufacturer model above is defined in another application called production, you'd need to use:

class Car(models.Model): 
    manufacturer = models.ForeignKey('production.Manufacturer') 

This sort of reference can be useful when resolving circular import dependencies between two applications.

5

कभी-कभी, django.db.models.get_model सर्कुलर आयात के आसपास काम करने में मदद करता है। जैसे मैं एक जवाब है, जो अधिक लगातार काम करता है :)

पाया है

from django.db.models import get_model 

class Entry(models.Model): 
    division = models.ForeignKey(get_model('competitions', 'Division')) 
+0

जो मैं देख सकता हूं उससे यह हमेशा काम नहीं करता है ... मुझे लगता है कि यह उस क्रम पर निर्भर करता है जिसमें django ऐप्स या कुछ ऐसा लोड करता है। फिर भी - बहुत उपयोगी युक्ति, धन्यवाद :) –

+0

इसलिए "कभी-कभी"; –

+0

ओउप्स, मेरा बुरा;) –