答案 0 :(得分:6)
答案 1 :(得分:2)
可以做到。 您需要先在admin.py中创建相应的modelAdmin。
对于列表“显示”过滤,修改queryset方法:
class MyModelAdmin(admin.ModelAdmin):
def queryset(self, request):
return Entry.objects.filter(owner=request.user)
对于字段过滤,根据您要限制的字段类型,您可以覆盖相应的方法。
相关的django文档在这里: https://docs.djangoproject.com/en/1.3/ref/contrib/admin/#django.contrib.admin.ModelAdmin.formfield_for_foreignkey
要限制foreignkey字段输出,您可以执行以下操作: (来自django文档)
class MyModelAdmin(admin.ModelAdmin):
def formfield_for_foreignkey(self, db_field, request, **kwargs):
if db_field.name == "car": # The name of the field you want to limit
kwargs["queryset"] = Car.objects.filter(owner=request.user)
return super(MyModelAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)