i have a many-to-many to it self, and i try access the same information by querying the [many-to-many reverse relationship] from my Model object, like in documentation. But not works, always get “object has no attribute”.
Its because its a many-to-many to it self? Is there a solution, or workaround?
A ManyToMany to self is going to have two Foreign Keys to the same model. When that occurs, you need to use the related_name attribute for each of those ForeignKey fields.
So remember that when you’re accessing a ForeignKey “backwards”, you need to refer to it by the related name of that foreign model. You need to tell Django which “direction” to follow to identify elements of that through table.
If you have an instance of ArticleVersion named article_version, then the set of Article_Version_Child that relates to article_version by the to_article_version FK field would be to_article_version.
instance of ArticleVersion = article_version / ringo
name of manytomany table _set = article_version_child_set / membership_set
get name of field and other instance = .get(from_article_version=article_child) / .get(group=beatles)
That analogy doesn’t directly apply. The membership set that you are referencing from the example does not use a related name attribute in its ForeignKey definition.
But i like to get from on instace. Like in the docs:
If you need to access a membership’s information you may do so by directly querying the Membership model:
>>> ringos_membership = Membership.objects.get(group=beatles, person=ringo)
>>> ringos_membership.date_joined
datetime.date(1962, 8, 16)
>>> ringos_membership.invite_reason
'Needed a new drummer.'
Another way to access the same information is by querying the many-to-many reverse relationship from a Person object:
>>> ringos_membership = ringo.membership_set.get(group=beatles)
>>> ringos_membership.date_joined
datetime.date(1962, 8, 16)
>>> ringos_membership.invite_reason
'Needed a new drummer.'
First sample i can do it, second no. Can you help to do it?
You access the through table entries using the related name manager for the aspect of the relationship you wish to use for the query.
It may help if you provide a tangible example for your models using a sample of the data that you are working with. You’re continuing to rely upon an example that does not directly apply to your situation.