I have the following models: http://slexy.org/view/s20T8yOiKZ
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',)
When I try to click add button for worlds in admin page it shows me the following error:
class 'cms_sample.world_models.Element' has more than 1 ForeignKey to class 'cms_sample.world_models.World'.
I think it's something to do with inline. What can it be?
Django doesn't know which of the two foreign keys (parent and world) is to be inlined using the ElementInline
.
class ElementInline(admin.TabularInline): model = Element fk_name = 'parent' #or 'world', as applicable. extra=1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With