Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我对 Django 很陌生,我正在实现 admin.py 文件。我想知道是否可以在 admin.py 文件中添加 if 语句。我想要做的是仅当某个变量为真时才在我的数据库中显示一行。这可能吗?谢谢。
只需覆盖ModelAdmin.queryset()(或ModelAdmin.get_queryset()1.6+)
ModelAdmin.queryset()
ModelAdmin.get_queryset()
class MyAdmin(admin.ModelAdmin): def queryset(self): qs = super(MyAdmin, self).queryset() if True: # however you get your condition return qs.filter() # whatever rows need to be included return qs.filter()