• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

In Django, how to create self-referencing, truly symmetrical ManyToMany relationship?

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!