Django管理在线显示,但外键具有相反的关系 [英] Django admin inline display but foreign key has opposite relation

查看:127
本文介绍了Django管理在线显示,但外键具有相反的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $

def __unicode __(self):
return self.summary


class Two(models.Model):
image = models.ImageField(upload_to =images)
title = models.CharField(max_length = 50)

def __unicode __(self):
return self.title


class Three(models.Model):



one = models.ForeignKey(One,related_name ='one_home')
two = models.ForeignKey (Two,related_name ='two_home')

def __unicode __(self):
return self.tile

现在我想要的是在Django Admin中显示一个和两个作为三行内容。



我搜索到任何地方,但内联显示是给予相反的关系。像三人的外键一样。不是我想要的另一种方式。

解决方案

相当简单:



你的模型是破碎的,三个可以只有一个和一个两个。



你可以有一个两个实体的三个列表。 p>

如果您反转外键,请更改他们所在的型号(均指向@ Three)

  class OneToThreeInline(admin.StackedInline)
fk_name ='three'
model = One

class TwoToThreeInline(admin.StackedInline):
fk_name ='three'
model = Two


class One(models.Model):
    image = models.ImageField(upload_to="images")
    summary = models.TextField()

    def __unicode__(self):
        return self.summary


class Two(models.Model):
    image = models.ImageField(upload_to="images")
    title = models.CharField(max_length=50)

    def __unicode__(self):
        return self.title


class Three(models.Model):



  one = models.ForeignKey(One, related_name='one_home')
    two = models.ForeignKey(Two, related_name='two_home')

    def __unicode__(self):
        return self.tile

Now what i want is to show One and Two as inlines of Three in Django Admin.

I searched everywhere but inline display is given for opposite relationship. like where one has the foreign key of Three. not the other way round which i want.

解决方案

rather simple:

Your model is "broken", three can have only 1 One and 1 Two.

you can have a list of three for both One an Two entities.

If you reverse the foreign keys, change the models they are on (both pointing @ Three)

class OneToThreeInline(admin.StackedInline):
    fk_name = 'three'
    model = One

class TwoToThreeInline(admin.StackedInline):
    fk_name = 'three'
    model = Two

这篇关于Django管理在线显示,但外键具有相反的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆