2010-03-09 20 views
33

मैं निम्नलिखित मॉडल: http://slexy.org/view/s20T8yOiKZ1 से अधिक विदेशी कुंजी

from mxutils.cms_services import generate_secid 
from django.db import models 
from django.contrib import admin 
from django import forms 

class World(models.Model): 
    title = models.CharField(max_length=150) 
    secid = models.SlugField(max_length=1000, editable=False) 
    elements = models.ManyToManyField("Element", related_name='elements', blank=True, null=True) 
    metadata = models.OneToOneField("Category_metadata", blank=True, null=True) 
    def save(self): 
     if not self.pk: 
      super(World, self).save() 
      self.secid = generate_secid(self.title, self.pk, World.objects.all()) 
     return super(World, self).save() 
    def __unicode__(self): 
     return "%s" % self.title 

class Element(models.Model): 
    parent = models.ForeignKey(World, related_name='element_parent') 
    world = models.ForeignKey(World, related_name='world', blank=True, null=True) 
    item = models.ForeignKey("Item", blank=True, null=True) 
    value = models.DecimalField(default=0, max_digits=5, decimal_places=3) 
    def save(self): 
     if self.world and self.item: 
      return None 
     elif not self.world and not self.item: 
      return None 
     else: 
      return super(Element, self).save() 
    def __unicode__(self): 
     if self.world: 
      return "%s" % self.world.title 
     else: 
      return "%s" % self.item.title 

class ElementInline(admin.TabularInline): 
    model = Element 
    extra=1 

class WorldAdmin(admin.ModelAdmin): 
    inlines = [ElementInline,] 
    list_display = ('title',) 
    ordering = ['title'] 
    search_fields = ('title',) 

जब मैं क्लिक करने के लिए व्यवस्थापक पृष्ठ में दुनिया के लिए बटन जोड़ने यह मुझे निम्न त्रुटि से पता चलता कोशिश:

class 'cms_sample.world_models.Element' has more than 1 ForeignKey to class 'cms_sample.world_models.World'.

मुझे लगता है कि यह इनलाइन के साथ कुछ करने के लिए है। यह क्या हो सकता है?

उत्तर

75

Django को पता नहीं है कि दो विदेशी कुंजी (माता-पिता और दुनिया) में से कौन सा ElementInline का उपयोग करके रेखांकित किया जाना है।

class ElementInline(admin.TabularInline): 
    model = Element 
    fk_name = 'parent' #or 'world', as applicable. 
    extra=1 
+2

'parent' (या' world') उद्धरण में –

+0

@Daniel धन्यवाद करने के लिए, तय की आवश्यकता होगी। – Amarghosh

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