[Answered ]-Most effective way to get number of items with similar value in a Django Query


First of all, if your model has two fields with the same relationship you must send a backwards name which is the related_name, like this:

class Contact(basemodel):

    item = models.ForeignKey(Item)
    recipient = models.ForeignKey(User,related_name='recipient_contact' )
    sender = models.ForeignKey(User,related_name='sender_contact' )

After that to retrive a list of senders you can do the Query in the actually sender(User), and just annotate the count:


I guess this will work! let me know anything!

Leave a comment