Win a copy of Event Streams in Action this week in the Java in General forum!

shrikant ph

Greenhorn
+ Follow
since Jul 23, 2019
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by shrikant ph

In Django, how to create self-referencing, truly symmetrical ManyToMany relationship with some additional fields under that relationship(in 2019)? Also, enable data entry in django admin and serialize it in DRF ?This is different than facebook friends or twitter followers.

I’ve a model that gives details about a stadium. Once user is on the page of stadium, there’ll be list of other stadiums which are in neighbouring vicinity to current stadium, along with their distances from current stadium. I want to implement this in django. So, suppose there are two stadiums , Stadium A and stadium B which are closer to each other. Then adding stadium B as neighbour of stadium A should automatically create stadium A as neighbour of stadium B.

This is different than facebook friends or twitter followers. In facebook, symmetrical relationship is not established as long as friend request isn’t accepted by the person whom request has been sent. So, person A can be said as friend of person B when he sends friend request to person B but reverse is not true if person B does not accept the request. In twitter followers, person A may follow person B but reverse may not be true. So, those are not truly symmetrical relationships. But, in my case, if two stadiums are in close proximity then they are each other’s neighbours.  Adding one as neighbour of another should automatically create reverse relation. I am not able to implement this in Django. I’ve referred article https://charlesleifer.com/blog/self-referencing-many-many-through/ on this. I’ve created a model where neighbour can be added but it’s not creating reverse relationship.

The model for stadium is:



Admin.py is:



Suppose there are two stadiums, 1. Old Trafford and 2. Etihaad Stadium.  These are neighbouring stadiums with distance of 12.5 km between them. When I add Etihaad Stadium as neighbour in Old Trafford model data using admin panel,
Old Trafford doesn't appear as neighbour in Etihaad stadium's model data. When I serialize all the data, only one model shows the neighbour and not the the other.

What am I doing wrong? Is there any better way in Django to solve self-referencing truly symmetrical manytomany relationship with extra fields in ‘through’ model? Is there any third party library for this?
4 weeks ago