188๐
You can override get_queryset
method in your model admin class.
class MyModelAdmin(admin.ModelAdmin):
def get_queryset(self, request):
qs = super().get_queryset(request)
if request.user.is_superuser:
return qs
return qs.filter(author=request.user)
Note in Django<=1.5 the method was named just queryset
.
10๐
Konrad is correct, but this is more difficult than the example given in the documentation.
Deleted conversations canโt be included in a queryset that already excludes them. So I donโt see an option other than re-implementing admin.ModelAdmin.queryset entirely.
class ConversationAdmin (admin.ModelAdmin):
def queryset (self, request):
qs = Conversation.all_conversations
ordering = self.get_ordering(request)
if ordering:
qs = qs.order_by(*ordering)
return qs
- [Django]-Django filter JSONField list of dicts
- [Django]-Bulk create model objects in django
- [Django]-How to customize activate_url on django-allauth?
7๐
You can do this with a Django proxy model.
# models.py
class UnfilteredConversation(Conversation):
class Meta:
proxy = True
# this will be the 'default manager' used in the Admin, and elsewhere
objects = models.Manager()
# admin.py
@admin.register(UnfilteredConversation)
class UnfilteredConversationAdmin(Conversation):
# regular ModelAdmin stuff here
...
Or, if you have an existing ModelAdmin class you want to re-use:
admin.site.register(UnfilteredConversation, ConversationAdmin)
This approach avoids issues that can arise with overriding the default manager on the original Conversation model โ because the default manager is also used in ManyToMany relationships and reverse ForeignKey relationships.
- [Django]-Django DRF with oAuth2 using DOT (django-oauth-toolkit)
- [Django]-Django model blank=False does not work?
- [Django]-Django logging of custom management commands
4๐
What would be so wrong with the following:
class Conversation(BaseModel):
...
deleted = models.BooleanField(default=False)
objects = models.Manager() # includes deleted conversations
nondeleted_conversations = NondeletedManager()
So in your own apps/projects, you use Conversation.nondeleted_conversations()
and let the built-in admin app do itโs thing.
- [Django]-Check if OneToOneField is None in Django
- [Django]-In a Django form, how do I make a field readonly (or disabled) so that it cannot be edited?
- [Django]-Django filter queryset __in for *every* item in list
4๐
Natan Yellin is correct, but you can change the managers order and the first will be the default, then it is the used by the admin:
class Conversation(BaseModel):
...
deleted = models.BooleanField(default=False)
all_conversations = models.Manager() # includes deleted conversations
objects = NondeletedManager()
The admin implementation of get_queryset()
use ._default_manager
instead .objects
, as show next
qs = self.model._default_manager.get_queryset()
ref Django github BaseModelAdmin implementation
This only ensures that every time you use YourModel.objects, you will not include deleted objects, but the generic views and others uses ._default_manager too. Then if you donโt override get_queryset is not a solution. Iโve just check on a ListView and admin.
- [Django]-Django 1.7 throws django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet
- [Django]-Django: Reference to an outer query may only be used in a subquery
- [Django]-Why does django's prefetch_related() only work with all() and not filter()?
2๐
The accepted solution works great for me but I needed a little bit more flexibility, so I ended up extending the changelist view to add in a custom queryset parameter. I can now configure my default queryset/filter as such and it can still be modified by using a different filter (get parameters):
def changelist_view(self, request, extra_context=None):
if len(request.GET) == 0 :
q = request.GET.copy()
q['status__gt'] = 4
request.GET = q
request.META['QUERY_STRING'] = request.GET.urlencode()
return super(WorksheetAdmin,self).changelist_view(request, extra_context=extra_context)
- [Django]-What's the difference between `from django.conf import settings` and `import settings` in a Django project
- [Django]-Data Mining in a Django/Postgres application
- [Django]-Django: TemplateDoesNotExist (rest_framework/api.html)
2๐
To extend on some of these answers with what I found most concise and useful.
Iโve made the assumption you have a field like "name" to show the entries.
# admin.py
from django.contrib import admin
@admin.register(Conversation)
class ConversationAdmin(admin.ModelAdmin):
list_display = ('name', '_is_deleted')
# Nice to have but indicates that an object is deleted
@admin.display(
boolean=True,
ordering='deleted'
)
def _is_deleted(self, obj):
return obj.deleted
def get_queryset(self, request):
return Conversation.all_conversations
Which will give you an interface like:
The problem I found with subclassing a model was that it caused issues with meta inheritance and reverse-path lookups.
- [Django]-Django-allauth social account connect to existing account on login
- [Django]-Can "list_display" in a Django ModelAdmin display attributes of ForeignKey fields?
- [Django]-What is a "django backend"?