Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/325.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 将选择限制为ForeignKey中特定模型类的所有对象_Python_Django_Django Models - Fatal编程技术网

Python 将选择限制为ForeignKey中特定模型类的所有对象

Python 将选择限制为ForeignKey中特定模型类的所有对象,python,django,django-models,Python,Django,Django Models,范例 如果只获取 某个类的对象?这不是很有效吗 class Base(): pass class A(Base) parent=models.Foreignkey("self", limit_choices_to=(all members of the B class) class B(Base) parent=models.Foreignkey("self", limit_choices_to=(all members of the A class) 是的,我认为OP的代码根本

范例

如果只获取
某个类的对象?

这不是很有效吗

class Base():
     pass

class A(Base)
parent=models.Foreignkey("self", limit_choices_to=(all members of the B class)

class B(Base)
parent=models.Foreignkey("self", limit_choices_to=(all members of the A class)

是的,我认为OP的代码根本不起作用-
self
不是
Base
,而是
A
B
。你是对的,我发布的代码是错误的。我想要的是类似于sebplq的响应(我认为这也不起作用,因为您不能覆盖子字段),但要维护父外键,只过滤A和B类的可用选项。有什么方法可以做到这一点吗?类基(Model):parent=models.Foreignkey(“self”)当您这样做并继承时,self是基,a或B。我想要的是将选择仅限于管理员的特定类名。
class Base(Model):
    parent=models.Foreignkey("self")

    class Meta:
        abstract = True


class A(Base):
    parent=models.Foreignkey("B")

class B(Base):
    parent=models.Foreignkey("A")