I’m trying to create a model representing graph node.
class Node(models.model):
ins = models.ManyToManyField("self", null=True, blank=True)
outs = models.ManyToManyField("self", null=True, blank=True)
Now let’s say we have nodes a and b. If I add a as in for b django (because of ‘symmetrical’ attribute on) will add b as in for a.
I have no idea how to do it the way I set a -> b it’s automatically set b <- a.
I thought about making some middle-class for relationship but don’t really see how it would work. Literally how to write it.
I’d see it something like that:
class Node(models.Model):
ins = models.ManyToManyField("self", null=True, blank=True, through="Edge")
outs = models.ManyToManyField("self", null=True, blank=True, through="Edge")
class Edge(models.Model):
node1_ins = models.ForeignKey(Node)
node2_outs = models.ForeignKey(Node)
But of course this one doesn’t work at all.
Any idea how to solve that one?
Thanks in advance,
Greg
You might want to try something like this:
I found that managing multiple m2m fields would require multiple “through” tables, which just becomes messy.
With this pattern you would set from-to relationships. The
outswould be explicit, and theinswould be a relation. Maybe this will work?The
edges_upandedges_downrelationships on the Node objects also let you find the relationship edge.The names here might be unclear. I kinda like the concept of “upstream” and “downstream”