#34354: Behaviour by design? models. Manager annotate() queryset conflict with
models.Model
-----------------------------------------+--------------------------
               Reporter:  David Pratten  |          Owner:  nobody
                   Type:  Bug            |         Status:  new
              Component:  contrib.admin  |        Version:  4.1
               Severity:  Normal         |       Keywords:  Possible
           Triage Stage:  Unreviewed     |      Has patch:  0
    Needs documentation:  0              |    Needs tests:  0
Patch needs improvement:  0              |  Easy pickings:  0
                  UI/UX:  0              |
-----------------------------------------+--------------------------
 With

 {{{
 class MyPapersManager(models.Manager):
      def get_queryset(self):
          return
 
super().get_queryset().annotate(count_of_keywords=Coalesce(Count('keywordspapers'),0))
 # .filter(in_scope=True) works fine here
 }}}

 {{{
 class MyPapers(models.Model):
     objects = MyPapersManager()
     count_of_keywords = models.IntegerField() # generated by manager
 above!
     keywordspapers = models.ManyToManyField(to='Keywords', blank=True)
 }}}

 and
 {{{
 class MyPapersAdmin(admin.ModelAdmin):
     list_display = ("count_of_keywords",)
 }}}


 Results in runtime error {{{The annotation 'count_of_keywords' conflicts
 with a field on the model.}}}

 However, f I remove the {{{count_of_keywords}}} field from the
 models.Model I get a start up error: {{{<class
 'literature.admin.MyPapersAdmin'>: (admin.E108) The value of
 'list_display[3]' refers to 'count_of_keywords', which is not a callable,
 an attribute of 'MyPapersAdmin', or an attribute or method on
 'literature.MyPapers'.}}}

 What is the correct way to add a computed field to a model in Admin land?

 Thanks

 David

-- 
Ticket URL: <https://code.djangoproject.com/ticket/34354>
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/010701866ce304e4-dfdb9561-a176-4752-be77-f6321dd685d1-000000%40eu-central-1.amazonses.com.

Reply via email to