Hi Derek,
On Sunday 15 January 2017 06:53:10 Derek wrote:
> You don't appear to have understood my question; and I did not see
> this particular use case covered in the docs (which is why I asked
> it). You also pointed me to the wrong version; in particular
> "QuerySet.as_manager()" is not method in 1.6.
Take a look at the DahlBookManager example: it filters all books by
author Dahl and "limits what is displayed".
If you compare it to your code, then you see that the entire
MyModelQuerySet class can be deleted and your
MyModelManager.get_queryset() method turns into:
def get_queryset(self):
return super(MyModelManager, self).get_queryset().filter(
classification='general'
)
Delete method could become something like this:
def delete(self):
qs = super(MyModelManager, self).get_queryset().filter(
code__neq='protected'
)
return qs.delete()
I'm using a call to super() in delete, so we get access to all objects.
Please read the part about the default manager and its dangers before
making it the default manager.
Last, but certainly not least: if you *only* need this for the admin,
then it's better to look at ModelAdmin's list_filter property and/or
get_queryset method and the has_delete_permission method. For example:
class MyModelAdmin(admin.ModelAdmin):
model = MyModel
list_filter = ('classification',)
def has_delete_permission(self, req, obj):
if super(MyModelAdmin, self).has_delete_permission(req, obj):
if obj.code != 'protected' :
return True
return False
Note that this doesn't set the default filter to 'general', but it does
make filtering transparent (and allows access to objects which are not
'general').
P.S.: The reason I pointed to 1.10 docs is simple: 1.6 is no longer
supported by the Django Project and it's documentation has been removed
from the site.
--
Melvyn Sopacua