Django admin list display optimize queryset - python

In User model i have a method:
#cached_property
def income(self):
return PartnerIncome.objects.all().aggregate(Sum('income'))['income__sum']*self.share_of_profit
PartnerIncome model:
class PartnerIncome(models.Model):
title = models.CharField(max_length=255)
income = models.FloatField(default=0)
Now i want to show 'inccome' in list_display=('income', ) at admin panel, but each object make extra query to database. How i can make PartnerIncome.objects.all().aggregate(Sum('income')) as a global variable for admin change list...

How about just overriding the get_queryset method in your ModelAdmin?
class UserAdmin(admin.ModelAdmin):
list_display = ('income', ...)
def income(self, obj):
return obj.income
def get_queryset(self, request):
queryset = super(UserAdmin, self).get_queryset(request)
# you logic here to `annotate`the queryset with income
return queryset
Documentation on aggregation:
https://docs.djangoproject.com/en/dev/topics/db/aggregation/
Without me really understanding your models and business logic.. here's a snippet from my own code as an example:
class AuthorAdmin(admin.ModelAdmin):
list_display = ('__str__', 'books_count',)
def books_count(self, obj):
return obj.books_count
def get_queryset(self, request):
return super(AuthorAdmin, self).get_queryset(
request).annotate(books_count=Count('books'))

Related

Overriding django admin get_queryset()

I have two models which is one of them proxy model.
In admin I registered both and overrided get_queryset() method but it is not working as expected.
admin.py
#admin.register(Category)
class CategoryAdmin(admin.ModelAdmin):
def get_queryset(self, request):
qs = super().get_queryset(request)
return qs.filter(language='en')
#admin.register(ProxyCategory)
class ProxyCategoryAdmin(CategoryAdmin):
def get_queryset(self, request):
qs = super().get_queryset(request)
return qs.filter(language='zh')
In admin page ProxyCateegoryAdmin not showing objects, if I remove get_queryset() from CategoryAdmin, it works but wanted filter both of them.
Thanks in advance
You can use self.model in the get_queryset method to filter the correct model.
#admin.register(Category)
class CategoryAdmin(admin.ModelAdmin):
def get_queryset(self, request):
qs = super().get_queryset(request)
if self.model == Category:
return qs.filter(language='en')
return qs
#admin.register(ProxyCategory)
class ProxyCategoryAdmin(CategoryAdmin):
pass
If you want ProxyCategoryAdmin to return both languages, do this:
#admin.register(ProxyCategory)
class ProxyCategoryAdmin(admin.ModelAdmin):
def get_queryset(self, request):
qs = super().get_queryset(request)
return qs.filter(language_in=['zh','en'])
When you inherit the CategoryAdmin, you end up with 2 filters: EN and ZH instead of EN or ZH

How to make customize detail view set using ModelViewSet?

I am new to django.
I have a model:
class Class(models.Model):
name = models.CharField(max_length=128)
students = models.ManyToManyField("Student")
def __str__(self) -> str:
return self.name
Now I want to create API to display the students in a particular class,the detail view, by giving the name of the class as a parameter using ModelViewClass.
Currently, I have following viewset written:
class ClassViewSet(ModelViewSet):
serializer_class = serializers.ClassSerializer
queryset = models.Class.objects.all()
How to do that?
You can use the #action(...) decorator to create a custom view.
from rest_framework.decorators import action
class ClassViewSet(ModelViewSet):
serializer_class = serializers.ClassSerializer
queryset = models.Class.objects.all()
#action(detail=True, methods=['get'], url_path='students', url_name='students')
def students(self, request, *args, **kwargs):
class_ = self.get_object()
students = class_.students.all()
serializer = serializers.StudentSerializer(students, many=True)
return Response(serializer.data)
If you are not using the DRF routers, you can specify the custom route as below,
urlpatterns = [
path('classes/', views.ClassViewSet.as_view({'get': 'list'})),
path('classes/<int:pk>/', views.ClassViewSet.as_view({'get': 'retrieve'})),
path('classes/<int:pk>/students/', views.ClassViewSet.as_view({'get': 'students'})),
]

How can I select an instance of django model to update the info inside that model or related models?

I have basically three models.
class Users(models.Model):
name = models.CharField(max_length=100)
...
class Portfolio(models.Model):
name = models.CharField(max_length=50)
user = models.ForeignKey(Users, on_delete=models.CASCADE, related_name='portfolio')
class BuySell(models.Model):
portfolio = models.ForeignKey(Portfolio, on_delete=models.CASCADE, related_name='buy_sell'
...
Any user can have multiple portfolios and portfolios can have many buy-sell. From my viewset how can I access the portfolio instance selected by the user to add buy-sell data?
In my Viewset:
class BuySellViewSet(
viewsets.GenericViewset,
mixins.ListModelMixin,
mixins.CreateModelMixin,
mixins.UpdateModelMixin,
mixins.DestroyModelMixin
):
serializer_class = BuySellSerializer
queryset = BuySell.objects.all()
def get_queryset(self):
return self.request.user.portfolio
But when I add multiple portfolios for a single user I get the following error message:
TypeError at /api/v1/share/buy-sell/
Field 'id' expected a number but got <django.db.models.fields.related_descriptors.create_reverse_many_to_one_manager.<locals>.RelatedManager object at 0x7f857c7c6940>.
How can I select the correct portfolio instance to add buy-sell data inside that instance? Also, How'd I be adding new buy-sell records in an instance of a portfolio from my viewset?
def create(self, request, *args, **kwargs):
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
share = serializer.validated_data["share"]
self.perform_create(serializer)
return Response({"data": serializer.data}, status=status.HTTP_201_CREATED)
You can filter with:
class BuySellViewSet(
viewsets.GenericViewset,
mixins.ListModelMixin,
mixins.CreateModelMixin,
mixins.UpdateModelMixin,
mixins.DestroyModelMixin
):
serializer_class = BuySellSerializer
def get_queryset(self):
return BuySell.objects.filter(portfolio__user=self.request.user)

How can I filter objects in my model to only show those of my ForeignKey user?

Trying to filter objects in my view.py to only show items (in my case Buckets) owned by Users.
I implemented the code below in my original Model [my model.py code is at the bottom of post]
class PostObjects(models.Manager):
def get_queryset(self):
return super().get_queryset().filter(status=Bucket.owner)
But I'm not sure if that is the correct procedure to list all items?
Here is the view.py where I'm trying to filter data by User aka owner. Users should ONLY be allowed to view their own items. (I will deal with permissions later)
class BucketList(generics.ListCreateAPIView):
queryset = Bucket.objects.all() #INSERT FILTER HERE
pass
Here is the model I'm referring too.
class Bucket(models.Model):
options = (
('personal', 'Personal'),
('social', 'Social'),
)
class PostObjects(models.Manager):
def get_queryset(self):
return super().get_queryset().filter(status=Bucket.owner)
owner = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='buckets')
users = models.ManyToManyField(settings.AUTH_USER_MODEL)
category = models.CharField(max_length=30, choices=options)
name = models.CharField(max_length=35)
created = models.DateTimeField(default=timezone.now)
slug = models.SlugField(unique=True, blank=True)
stock_count = models.IntegerField(blank=True, null=True)
stock_list = ArrayField(models.CharField(max_length=6),size=10)
objects = models.Manager()
postobjects = PostObjects()
class Meta:
ordering = ('-created',)
def total_stocks_calc(self):
self.stock_count = Bucket.objects.aggregate(Sum('stock_list', distinct=True))
self.save()
def get_absolute_url(self):
return reverse("bucket:bucket-view", kwargs={"slug": self.slug})
def __str__(self):
return self.stock_list
To re-state my question, how can I filter objects owned by users in class BucketList for their private view only?
UPDATE:
from django.db.models import Q
from rest_framework import generics
from bucket.models import Bucket
from .serializers import BucketSerializer
class OwnerOrUserFilterBackend(filters.BaseFilterBackend):
queryset = Bucket.postobjects.all() # wondering if you didnt write this for brevity reasons or because its not need due to the class?
def filter_queryset(self, request, queryset, view):
return queryset.filter(
Q(owner=request.user) | #do I not need to change one of the filters too request.owner?
Q(users=request.user)
)
class BucketList(generics.ListCreateAPIView):
model = Bucket
filter_backends = [OwnerOrUserFilterBackend]
You can override the get_queryset method and filter with self.request.user:
class BucketList(generics.ListCreateAPIView):
model = Bucket
def get_queryset(self, *args, **kwargs):
return super.get_queryset(*args, **kwargs).filter(
owner=self.request.user
)
For an API view, it probably is however better to define a filter, and then use this over all the API views where you want to apply this:
class IsOwnerFilterBackend(filters.BaseFilterBackend):
def filter_queryset(self, request, queryset, view):
return queryset.filter(owner=request.user)
Then you can use this in your ListCreateAPIView with:
class BucketList(generics.ListCreateAPIView):
model = Bucket
filter_backends = [IsOwnerFilterBackend]
The advantage of this is that if you have other views that require the same filtering, you only need to add the IsOwnerFilterBackend as filter_backend.
Another filter could include both the owner and the users as people who can see the BucketList:
from django.db.models import Q
class OwnerOrUserFilterBackend(filters.BaseFilterBackend):
def filter_queryset(self, request, queryset, view):
return queryset.filter(
Q(owner=request.user) |
Q(users=request.user)
)
then we thus filter with this filter:
class BucketList(generics.ListCreateAPIView):
model = Bucket
filter_backends = [OwnerOrUserFilterBackend]

Filtering objects in django admin

I want to manage objects in django admin, though I would like to be able only to edit objects with a specific value of some attribute. Precisely I have now in admin.py:
class UnitAdmin(admin.ModelAdmin):
list_display = ('type', 'name', 'result_file')
list_filter = ['type']
admin.site.register(Unit, UnitAdmin)
And I would like to manage only units with type='SomeSpecificType'. I saw something with overriding SimpleListFilter class, though I can't see how this applies here.
You have to override the get_queryset in de modelAdmin and filter objects that have type='SomeSpecificType.
class UnitAdmin(admin.ModelAdmin):
...
def get_queryset(self, request):
qs = super(UnitAdmin, self).get_queryset(request)
return qs.filter(type='SomeSpecificType')
You can do
class UnitAdmin(admin.ModelAdmin):
list_display = ('type', 'name', 'result_file')
list_filter = ['type']
def get_readonly_fields(self, request, obj=None):
if obj and obj.type == 'SomeSpecificType':
return []
return ["type", "name", "result_file"]

Categories