Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/346.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-表格内联外键字段列表的动态值_Python_Django_Django Models_Django Templates_Django Admin - Fatal编程技术网

Python Django Admin-表格内联外键字段列表的动态值

Python Django Admin-表格内联外键字段列表的动态值,python,django,django-models,django-templates,django-admin,Python,Django,Django Models,Django Templates,Django Admin,我有三种型号: class Foo (models.Model): date = models.DateField(default = date.today) class Bar (models.Model): foo = models.ForeignKey(Foo) another = models.ForeignKey(AnotherClass) #this class is important amount = models.IntegerField(de

我有三种型号:

class Foo (models.Model):
    date = models.DateField(default = date.today)

class Bar (models.Model):
    foo = models.ForeignKey(Foo)
    another = models.ForeignKey(AnotherClass) #this class is important
    amount = models.IntegerField(default=0)

class AnotherClass (models.Model):
    name = models.CharField()
现在我想以表格内联方式添加/更改Foo+Bar的实例。关键是每个Bar实例都是另一个类实例的引用,每个Foo实例都必须具有系统中所有另一个类的引用

如果我有一个其他类的实例列表

AnotherClass1
AnotherClass2
AnotherClass3
AnotherClass4
Foo添加/更改管理页面应该是这样的

Add Foo!
--------------------------------------------

date = (datePicker)

Pretty Bar TabularInline
--------------------------------------------
AnotherClass1.name        amount = intPicker
AnotherClass2.name        amount = intPicker
AnotherClass3.name        amount = intPicker
AnotherClass4.name        amount = intPicker
如果AnotherClassX.name不可编辑,则仅显示AnotherClass实例的名称


有什么简单的方法可以做到这一点吗?

您可以使用django.contrib.admin中的TabularInline执行类似的操作。下面是一个例子。我在与models.py相同的Django应用程序(“test_应用程序”)中创建了admin.py,只是为了将管理站点的内容与模型代码分开

### models.py ###
from django.db import models
from datetime import date

class Foo (models.Model):
    date = models.DateField(default = date.today)

class AnotherClass (models.Model):
    name = models.CharField(max_length=128)

    def __unicode__(self):
        return u'%s' % (self.name)

class Bar (models.Model):
    foo = models.ForeignKey(Foo)
    another = models.ForeignKey(AnotherClass) #this class is important
    amount = models.IntegerField(default=0)

    class Meta:
        unique_together = (('foo', 'another'),)

### admin.py ###
from test_app.models import Foo, Bar, AnotherClass
from django.contrib import admin

class BarsInline(admin.TabularInline):
    model = Bar

class FooAdmin(admin.ModelAdmin):
    inlines = (BarsInline,)

admin.site.register(Foo, FooAdmin)
admin.site.register(Bar)
admin.site.register(AnotherClass)
这将增加在创建或编辑Foo实例时添加或修改多个条形图关系的功能。请注意,AnotherClass选项是下拉菜单,但AnotherClass.name字段不可编辑


需要注意的一个问题是,您描述的模型模式允许同一个其他类实例与一个Foo(通过Bar)多次关联。这可能不是你想要的。要防止另一个类实例和一个Foo实例之间存在多个关系,请在条形图模型中使用元选项“unique_together”,这在上面的示例代码中使用。如果要允许此行为,只需删除此元类选项。

我尝试了此操作,但我不希望用户每次都通过下拉菜单选择其他所有类实例,但每次用户要添加/更改Foo实例时,它们都会出现,而无需选择它们。我不得不说,我不知道‘unique_together’属性,我认为它非常有用,但我担心的另一部分是,仅使用admin Django应用程序是不可能做到的。