remy
remy

Reputation: 4743

Django. How to get related_name of a field from the contenttypes framework

How can I retrieve a related_name of a known field using content types framework?

I have reached as far as getting the actual field. But I have no idea how to lay hands on it's arguments.

field = content_type.model_class()._meta.get_field_by_name('field_name')
related_name = ?

Upvotes: 3

Views: 1777

Answers (2)

ChrisRob
ChrisRob

Reputation: 1552

Thanks gwaramadze! Here is a similar answer if you have an object:

object._meta.get_field(field_name).related_query_name()

Upvotes: 0

remy
remy

Reputation: 4743

OK, got it. As a reminder, field_name is what I know, it's hardcoded.

related_name = content_type.model_class().field_name.field.related_query_name()

Upvotes: 6

Related Questions