The annotation 'xy' conflicts with a field on the model - django

django, django-models, python

Solution

The problem is that `tc_books` is also the related name for your books. Also, you're trying to use `Sum` on a non-numerical value. You should use `Count` instead. Try this:

Topiccenter.objects.annotate(num_books=Count('tc_books'),
                             num_journals=Count('tc_journals')
                             ).extra(
                                 select={'sum_entries':'num_books+num_journals'}, 
                                 order_by=('sum_entries',)
                             )

Problem

I have models: ``` class Topiccenter(models.Model): name = models.TextField() def sumentries(self): return self.tc_books.count() + self.tc_journals.count() class Book(models.Model): name = models.TextField() tc = models.ForeignKey(Topiccenter,related_name="tc_books") class Journal(models.Model): name = models.TextField() tc = models.ForeignKey(Topiccenter,related_name="tc_journals") ``` I am trying to do something like this: ``` Topiccenter.objects.annotate(tc_books=Sum('tc_books'), journals=Sum('tc_journals') ).extra( select={'sum_entries':'tc_books+journals'}, order_by=('sum_entries',) ) ``` but it is saying: `The annotation 'tc_books' conflicts with a field on the model.`

Original source