Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用Django Admin中内联的OneToOneField_Python_Django_Django Admin - Fatal编程技术网

Python 使用Django Admin中内联的OneToOneField

Python 使用Django Admin中内联的OneToOneField,python,django,django-admin,Python,Django,Django Admin,我有带有LogOutput对象的数据文件模型。数据文件对象将有一个登录输出。对于属于数据文件的任何注销输出,它将只属于单个数据文件。其他模型也有LogOutput对象 由于它们是一对一的,除了LogOutput可以属于数据文件以外的东西(例如,套件也有它们——请参见下面的代码),我认为正确的做法是在数据文件中定义一个OneToOneField,即LogOutput 型号.py: class LogOutput(models.Model): raw = models.TextField()

我有带有LogOutput对象的数据文件模型。数据文件对象将有一个登录输出。对于属于数据文件的任何注销输出,它将只属于单个数据文件。其他模型也有LogOutput对象

由于它们是一对一的,除了LogOutput可以属于数据文件以外的东西(例如,套件也有它们——请参见下面的代码),我认为正确的做法是在数据文件中定义一个OneToOneField,即LogOutput

型号.py

class LogOutput(models.Model):
    raw = models.TextField()

class DataFile(models.Model):
    name = models.CharField()#etc.
    logoutput = models.OneToOneField(LogOutput)

class Suite(models.Model):
    # Many Suites will have the same datafile:
    datafile = models.ForeignKey(DataFile)

    # Each Suite has a unique LogOutput different from the DataFile's
    # and as with the DataFile, that LogOutput will have just one Suite
    logoutput = models.OneToOneField(LogOutput)
class LogOutputInline(admin.TabularInline):
    model = LogOutput

class DataFileAdmin(admin.ModelAdmin):
    search_fields = ['name']
    inlines = [LogOutputInline]

admin.site.register(DataFile, DataFileAdmin)
现在,当我在Admin视图中查看一个数据文件时,我希望看到LogOutput,所以我想我应该直接内联它

admin.py

class LogOutput(models.Model):
    raw = models.TextField()

class DataFile(models.Model):
    name = models.CharField()#etc.
    logoutput = models.OneToOneField(LogOutput)

class Suite(models.Model):
    # Many Suites will have the same datafile:
    datafile = models.ForeignKey(DataFile)

    # Each Suite has a unique LogOutput different from the DataFile's
    # and as with the DataFile, that LogOutput will have just one Suite
    logoutput = models.OneToOneField(LogOutput)
class LogOutputInline(admin.TabularInline):
    model = LogOutput

class DataFileAdmin(admin.ModelAdmin):
    search_fields = ['name']
    inlines = [LogOutputInline]

admin.site.register(DataFile, DataFileAdmin)
似乎由于OneToOneField(s)的定义方向性,我无法进行内联。上面的admin.py为我提供了:

<class 'trial.models.LogOutput'> has no ForeignKey to <class 'trial.models.DataFile'>
没有要删除的外键
这当然是真的,但我不知道它有什么关系,因为一个数据文件有一个(而且只有一个)LogOutput,而这个LogOutput又只属于这一个数据文件

我读过,但解决办法是扭转关系的方向。我认为我不能这样做,因为其他对象(套件)也有注销输出

如果有必要的话,这是Django 1.5版本

我的问题是:为了在数据文件的管理页面上显示内联LogOutput,我需要做什么?(或者我的想法是使用OneToOneField需要修改?)


蒂亚

内部ORM和admin非常智能,但是OOP遇到表结构时,可能会有点不精确。我建议您使用如下方法给ORM一点提示:

class LogOutput(models.Model):
    raw = models.TextField()

    class Meta:
        abstract = True

class DataFileLogOutput(LogOutput):
    pass

class SuiteFileLogOutput(LogOutput):
    pass

class DataFile(models.Model):
    name = models.CharField()#etc.
    logoutput = models.OneToOneField(DataFileLogOutput)

class Suite(models.Model):
    # Many Suites will have the same datafile:
    datafile = models.ForeignKey(DataFile)

    # Each Suite has a unique LogOutput different from the DataFile's
    # and as with the DataFile, that LogOutput will have just one Suite
    logoutput = models.OneToOneField(SuiteLogOutput)

还有一个django插件,可以在不反转OneToOneField方向的情况下进行内联:

您需要将django_reverse_admin添加到requirements.txt:

-e git+https://github.com/anziem/django_reverse_admin.git#egg=django_reverse_admin
然后导入它:

管理员

from django_reverse_admin import ReverseModelAdmin

# don't need to define an inline for LogOutput

class DataFileAdmin(ReverseModelAdmin):
    search_fields = ['name']
    inline_reverse = ['logoutput']
    inline_type = 'tabular'  # or could be 'stacked'

admin.site.register(DataFile, DataFileAdmin)

你试过fk_的名字吗?fk_name=“logoutput”我不熟悉它。我来看看,似乎是同样的问题。具体地说,方向性仍然被认为是另一种方式:LogOutputInline.fk_name'指的是模型'trial'中缺少的字段“”。logoutputine似乎是它的正确行为:@Anto-问题本身已经提到了可能的重复项以及为什么它不适用。是的,我希望避免这种情况,但我可以看出,我的思维模式可能与ORM管理SQL的方式不一致。谢谢你的指点!这如何允许在admin中反转logoutput-OneToOne关系?(关于管理如何解决关系的方向性)