#25396: ModelAdmin.get_queryset doesn't accept ValueQuerySet
-------------------------------+--------------------------------------
     Reporter:  dex4er         |                    Owner:  nobody
         Type:  New feature    |                   Status:  closed
    Component:  contrib.admin  |                  Version:  master
     Severity:  Normal         |               Resolution:  duplicate
     Keywords:                 |             Triage Stage:  Unreviewed
    Has patch:  1              |      Needs documentation:  0
  Needs tests:  0              |  Patch needs improvement:  0
Easy pickings:  0              |                    UI/UX:  0
-------------------------------+--------------------------------------

Comment (by dex4er):

 I'm not sure what do you mean "different API".

 I've refactored this example use case and created "database view" with
 `model.Manager`. The fields are aliased with
 `annotate(aliased_field=F('original_field')).

 This solution works also with django-import-export, so I can generate CSV
 or XLS file based on this aggregated `QuerySet`!

 models.py

 {{{#!python
 from django.db import models
 from django.db.models import Count, F
 from django.contrib.auth.models import User


 class Category(models.Model):
     name = models.CharField(max_length=100)

     class Meta:
         verbose_name_plural = 'categories'

     def __unicode__(self):
         return self.name


 class Note(models.Model):
     user = models.ForeignKey(User)
     category = models.ForeignKey(Category)
     keywords = models.CharField(max_length=400)
     title = models.CharField(max_length=200)
     date = models.DateTimeField()
     content = models.TextField(max_length=50000)

     def __unicode__(self):
         return self.title


 class UserNotesReportManager(models.Manager):
     def get_queryset(self):
         return Note.objects \
             .annotate(user_username=F('user__username'),
                       category_name=F('category__name')) \
             .values('user_username', 'category_name') \
             .order_by('user_username', 'category_name') \
             .annotate(n=Count('*'))


 class UserNotesReport(models.Model):
     user_username = models.CharField(max_length=30, primary_key=True)
     category_name = models.CharField(max_length=100, primary_key=True)
     n = models.IntegerField()

     objects = UserNotesReportManager()
 }}}

 admin.py

 {{{#!python
 from django.contrib import admin

 import hello_blog.models as models


 class CategoryAdmin(admin.ModelAdmin):
     pass

 admin.site.register(models.Category, CategoryAdmin)


 class NoteAdmin(admin.ModelAdmin):
     pass

 admin.site.register(models.Note, NoteAdmin)


 class UserNotesReportAdmin(admin.ModelAdmin):
     actions = None
     list_display = ['user_username', 'category_name', 'n']
     list_display_links = None
     readonly_fields = list_display

     def has_add_permission(self, request):
         return False

 admin.site.register(models.UserNotesReport, UserNotesReportAdmin)
 }}}

 This example is available at https://github.com/dex4er/django-hello-
 world/tree/blog_reports so you can check if the modification works
 correctly.

--
Ticket URL: <https://code.djangoproject.com/ticket/25396#comment:8>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" 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].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/064.afb217afb386a508ec939ad21491fc50%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to