在 Django 模型中 limit_choices_to 中是否可以有两个参数?
is it possible to have two parameters in limit_choices_to in django models?
我会尽可能简化代码。据说,我们确实有两个模型。
models.py > 产品 table
CATEGORY = (
('Hard Disk Drive', 'Hard Disk Drive'),
('Solid State Drive', 'Solid State Drive'),
('Graphics Card', 'Graphics Card'),
('Laptop', 'Laptop'),
('RAM', 'RAM'),
('Charger', 'Charger'),
('UPS', 'UPS'),
('Mouse', 'Mouse'),
('Keyboard', 'Keyboard'),
('Motherboard', 'Motherboard'),
('Monitor', 'Monitor'),
('Power Supply', 'Power Supply'),
('Router', 'Router'),
('AVR', 'AVR'),
('Tablet', 'Tablet'),
('System Unit', 'System Unit'),
('Audio Devices', 'Audio Devices'),
('CPU', 'CPU'),
('Others', 'Others'),
)
class Product(models.Model):
model_name = models.CharField(max_length=100, null=True, blank=True)
asset_type = models.CharField(max_length=20, choices=CATEGORY, blank=True)
date = models.DateField(null=True, blank=True)
和另一个table>订单table
class Order(models.Model):
product_order = models.ForeignKey(Product, on_delete=models.CASCADE, null=False)
employee = models.ForeignKey(User, models.CASCADE, null=False)
date = models.DateTimeField(auto_now_add=True)
remarks = models.TextField()
而且我们都知道添加这段代码会限制订单下的外键选择。
limit_choices_to={"asset_type": "Hard Disk Drive"}
limit_choices_to={"asset_type": "Solid State Drive"}
我的目标是显示产品 table 中 asset_type 是“硬盘驱动器”或“固态驱动器”的项目。我已经阅读了 Django 的“limit_choices_to”文档,但在这里看不到任何与某种解决方案有关的内容。预先感谢您知道实现这一目标的方法。
您可以使用 __in
lookup [Django-doc] 指定允许值的 列表:
class Order(models.Model):
product_order = models.ForeignKey(
Product,
on_delete=models.CASCADE,
<b>limit_choices_to=dict(asset_type__in=['Hard Disk Drive', 'Solid State Drive'])</b>
)
# …
Note: Specifying null=False
[Django-doc] is not necessary: fields are by default not NULLable.
Note: It is normally better to make use of the settings.AUTH_USER_MODEL
[Django-doc] to refer to the user model, than to use the User
model [Django-doc] directly. For more information you can see the referencing the User
model section of the documentation.
我会尽可能简化代码。据说,我们确实有两个模型。
models.py > 产品 table
CATEGORY = (
('Hard Disk Drive', 'Hard Disk Drive'),
('Solid State Drive', 'Solid State Drive'),
('Graphics Card', 'Graphics Card'),
('Laptop', 'Laptop'),
('RAM', 'RAM'),
('Charger', 'Charger'),
('UPS', 'UPS'),
('Mouse', 'Mouse'),
('Keyboard', 'Keyboard'),
('Motherboard', 'Motherboard'),
('Monitor', 'Monitor'),
('Power Supply', 'Power Supply'),
('Router', 'Router'),
('AVR', 'AVR'),
('Tablet', 'Tablet'),
('System Unit', 'System Unit'),
('Audio Devices', 'Audio Devices'),
('CPU', 'CPU'),
('Others', 'Others'),
)
class Product(models.Model):
model_name = models.CharField(max_length=100, null=True, blank=True)
asset_type = models.CharField(max_length=20, choices=CATEGORY, blank=True)
date = models.DateField(null=True, blank=True)
和另一个table>订单table
class Order(models.Model):
product_order = models.ForeignKey(Product, on_delete=models.CASCADE, null=False)
employee = models.ForeignKey(User, models.CASCADE, null=False)
date = models.DateTimeField(auto_now_add=True)
remarks = models.TextField()
而且我们都知道添加这段代码会限制订单下的外键选择。
limit_choices_to={"asset_type": "Hard Disk Drive"}
limit_choices_to={"asset_type": "Solid State Drive"}
我的目标是显示产品 table 中 asset_type 是“硬盘驱动器”或“固态驱动器”的项目。我已经阅读了 Django 的“limit_choices_to”文档,但在这里看不到任何与某种解决方案有关的内容。预先感谢您知道实现这一目标的方法。
您可以使用 __in
lookup [Django-doc] 指定允许值的 列表:
class Order(models.Model):
product_order = models.ForeignKey(
Product,
on_delete=models.CASCADE,
<b>limit_choices_to=dict(asset_type__in=['Hard Disk Drive', 'Solid State Drive'])</b>
)
# …
Note: Specifying
null=False
[Django-doc] is not necessary: fields are by default not NULLable.
Note: It is normally better to make use of the
settings.AUTH_USER_MODEL
[Django-doc] to refer to the user model, than to use theUser
model [Django-doc] directly. For more information you can see the referencing theUser
model section of the documentation.