Comment: GPGTools - https://gpgtools.org
iQIcBAEBCgAGBQJUM6v2AAoJEB267zEX1rwQggkP/iHNjePEU8LgW1a9OR+3PPKE
/g9tj1+8gj5gcmkNTL4mwq+K//SKbdjoSGtu89RVqIG82hWxHveMS2aQxSHkWayx
lSuaN7ooraL4cyI5dGaC2tzPDO08SYNp31DHeLvyCIaVC7r4/7K2BdlQN8iR6/DB
tqNG+4GHAcd+44e+gGamQDA/xQLGaOVGxtgiqxHEVil3LOs/P4OZKOvKt6kHtXnY
p3SNR/yWjpdKq2H9INj3GtAiofVB/aB9gbCHza/9atL2dM4bBqWIUrkWjOG9HYGe
jd8pf+pcDksqlNWNokH6yMC3I1prGgEzGeXRfyU7nnorLQ3BvyMk0+0Cc6mudwz3
k3Guzuv2nDZdtk8vefzEYJI8tfE/1yLIFvF/kcIKhakEJviwQ0aLx8oTR7zy8mp/
md21yRY+EEM0IewPV3cZ1UVjJoqzlvgZMeos/C9ah2R3r1WaLtgqn5AeAw8qSF1c
sbJmlB+YvEw+4+VVFlhiW6eixmeim2INDq3p3JNzofP0phsjMUf2cxoiAnK+0kpY
MBwc584tkwOn2g7ML2CcPY7E2fS4k+EmwM1llnJCPEbK5E2rJWRPJpNwwrUAmHAj
zxnJk/uVQDjsKG4GESfncKQG9m67Hb98C951gViQiVW4e4RYm6OuLttpugGckkU8
Sr5wuNnPct6lYFFLeQKr
=rtxq
-----END PGP SIGNATURE-----
Hi Andrea,
Let's suppose I have a
Fooapp with aBarmodel with aownerfield. I want a user to be able to edit all the instances for whichobj.owner == request.user.The model appears correctly in the admin panel for superusers and for users for which I have explicitly assigned the permission
change_baroradd_bar, as explained here:Assuming you have an application with an app_label foo and a model named Bar, to test for basic permissions you should use:
add: user.has_perm('foo.add_bar') change: user.has_perm('foo.change_bar') delete: user.has_perm('foo.delete_bar')How can I show the model
Barin the admin index list without explicitly assigningfoo.change_barorfoo.add_barto the user?
So far I tried the following, expecting the
Barmodel to appear in the index list page, but it didn't work.class BarAdmin(admin.ModelAdmin): def get_queryset(self, request): qs = super(BarAdmin, self).get_queryset(request) if request.user.is_superuser: return qs return qs.filter(owner=request.user) def has_add_permission(self, request): return True def has_change_permission(self, request, obj=None): if obj is None: return True if obj.owner == request.user: return True return False def has_delete_permission(self, request, obj=None): if obj is None: return True if obj.owner == request.user: return True return False def has_module_permission(self, request): return TrueAccessing the link
admin/foo/bar/works correctly for every user and returns the list ofBarinstances for whichobj.owner == request.user.admin/foo/bar/addallows the user to add a new object correctly. These links are although not displayed in the admin index page: which is the function that triggers the appearance of the model in the index page?admin/foo/returns403 Forbidden.
I'm using Django 1.7
Thanks,
Andrea
No comments:
Post a Comment