Django admin内联显示但外键有相反的关系 [英] Django admin inline display but foreign key has opposite relation

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

问题描述

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

现在我想要在 Django Admin 中将一和二显示为三的内联.

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.

推荐答案

比较简单:

你的模型坏了",三个只能有1个1和1个2.

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 admin内联显示但外键有相反的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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