gpt4 book ai didi

django - 多于 1 个外键

转载 作者:行者123 更新时间:2023-11-28 19:35:46 28 4
gpt4 key购买 nike

我有以下型号: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',)

当我尝试在管理页面中单击世界添加按钮时,它显示以下错误:

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

我认为这与内联有关。它可以是什么?

最佳答案

Django 不知道要使用 ElementInline 内联两个外键(父键和世界键)中的哪一个。

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

关于django - 多于 1 个外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2408989/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com