I am looking for help with a Django 1.6 application.
The use case is not too complicated; I need to override the delete method
for
the queryset to prevent some records being removed by the bulk delete (run
via
the admin interface); and I also want to override the base filter for the
model's
queryset to limit what is displayed.
Preventing the deletion works well; but I cannot see how to override the
base
filter for the model - below are some examples of code I have tried that do
not
work.
I am sure this is not hard, but I cannot find the correct syntax.
Any ideas appreciated.
Thanks
Derek
```
from django.db.models import Model, Manager
from django.db.models import AutoField, CharField
from django.db.models.query import QuerySet
class MyModelQuerySet(QuerySet):
# NEED to override the base filter for this model ???
"""
def __init__(self, *args, **kwargs):
super(MyModelQuerySet, self).__init__(*args, **kwargs)
# does not filter at all?
self.queryset = models.Site.objects.filter(
classification='general')
def filter(self):
# just gives errors!
return self.queryset.filter(classification='special')
"""
def delete(self, *args, **kwargs):
for obj in self:
if obj.code != 'protected':
obj.delete()
class MyModelManager(Manager):
def get_queryset(self):
return MyModelQuerySet(model=self.model, using=self._db)
class MyModel(Model):
id = AutoField(primary_key=True)
name = CharField(
unique=True,
max_length=50,)
code = CharField(
unique=True,
max_length=250)
classification = CharField(
max_length=100)
objects = MyModelManager()
```
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/CAF1Wu3Mfo08k17rZvbAk3Nvv_nuSvr_SOD%3DSX%2B5JJaEqKyWu5Q%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.