社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Django

如何删除django管理中列表输入的某些元素

Valentin Garreau • 5 年前 • 1941 次点击  

我的django管理员有一个这样的列表: enter image description here

你知道是否可以删除列表中的某些元素吗?例如,我想删除所有状态为“inactive”(状态=False)的“gateway”。

class Gateway(models.Model):
    def __str__(self):
        return self.name

    name = models.CharField(max_length=255)
    logo = models.TextField()
    status = models.Boolean()

我甚至不知道是否有可能在django管理员的列表上做一些筛选。。。

谢谢你的帮助!

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/55450
 
1941 次点击  
文章 [ 1 ]  |  最新文章 5 年前
Willem Van Onsem
Reply   •   1 楼
Willem Van Onsem    5 年前

是的 . 你可以使用 limit_choices_to=… parameter [Django-doc] ForeignKey . 例如:

from django.db.models import Q

class MyModel(models.Model):
    gateway = models.ForeignKey(
        Gateway,
        on_delete=models.CASCADE,
        limit_choices_to=Q(status=True)
    )

所以我们在这里过滤候选集 Gateway 因此 status 字段应该是 True