[Fixed]-Django get custom m2m field value

1đź‘Ť

The similarity field is on RelatedQuestion while you’re trying to access it on a Question. You also can’t get it via the field because there are potentially many questions that are related; Which one would it choose to return the similarity value from?

Additionally, you can’t access the “through” model via the manager, which is why you’re getting errors on the ManyRelatedManager.

You can access the through model via a _set reverse lookup.

Question.objects.get(pk=5757).source_set.first().similarity

NOTE: I can’t test the above code as I write this, so take it with a grain of salt as it points you in the right direction.

👤Soviut

Leave a comment