{{{
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.