简体   繁体   English

如何制定此ForeignKey方案?

[英]How can I formulate this ForeignKey scheme?

I'm finding some difficulty resolving this problem in designing my Django models. 我发现在设计Django模型时解决此问题有些困难。

The essential setup is a bipartite graph allowing for parallel edges. 基本设置是允许平行边缘的二部图。 Trying our hardest to keep vertices in a single class, what is the most succinct means we have for querying all the edges incident to a vertex? 尽最大努力将顶点保持在一个类中,最简洁的含义是我们要查询入射到顶点的所有边吗?

The instruction to keep vertices encoded by a single class comes from a (possibly misguided) urgency I have to consolidate vertices within a signle database. 保持顶点由单个类编码的指令来自于(可能是误导的)紧急性,我必须将顶点合并到信号数据库中。

I considered using an inheritance scheme, wherein vertices in a partition inherit from a polymorphic parent Vertex class, but I kept tripping up, and really started worrying about whether Django had some (unkown to me) native and concise means of doing this. 我考虑使用继承方案,其中分区中的顶点从多态父Vertex类继承,但是我一直绊脚石,并真正开始担心Django是否具有某些(对我而言不为人所知的)本地且简洁的方法。

The current setup is about: 当前设置约为:

class Vertex(models.Model):
    PARTITION_CHOICES = (
        ('a': 'type a'),
        ('b': 'type b')
    )
    partition = models.CharField(choices=PARTITION_CHOICES)
    # A bunch of other attributes...

class Edge(models.Model):
    vertex_a = models.ForeignKey(Vertex, limit_choices_to={'partition': 'a'}, related_name='edges')
    vertex_b = models.ForeignKey(Vertex, limit_choices_to={'partition': 'b'}, related_name='edges')
    # More attributes...

The obvious issue here is that both these foreign keys have the same related name, therefore clash . 这里明显的问题是这两个外键具有相同的相关名称,因此为clash Otherwise, I would be able to quite neatly ask for vertex.edges.all() . 否则,我可以很整洁地要求vertex.edges.all()

Tips from the wise? 明智的秘诀?

You ask for tips from the wise, I'm not too wise, but I'll do my best. 您从明智者那里寻求技巧,我不太明智,但我会尽力而为。

For your related names, just make them the same as the field name, or dispense with them altogether. 对于您的相关名称,只需使它们与字段名称相同,或完全省去它们即可。 What is `related_name` used for in Django? 在Django中,“ related_name”是用来做什么的?

I think there maybe easier ways to query where the vertices are used, but the only way I can think of is something like this: 我认为可能有更简单的方法来查询使用顶点的位置,但是我能想到的唯一方法是这样的:

for edge in Edge.objects.all():
   vertex_a = edge.vertex_a
   vertex_b = edge.vertex_b

If you want to have more than two vertices per edge (or just two with one field name), you can use the ManyToManyField. 如果您希望每个边缘有两个以上的顶点(或者只有两个具有一个字段名),则可以使用ManyToManyField。 https://docs.djangoproject.com/en/2.0/topics/db/examples/many_to_many/ https://docs.djangoproject.com/en/2.0/topics/db/examples/many_to_many/

class Vertex(models.Model):
    PARTITION_CHOICES = (
        ('a': 'type a'),
        ('b': 'type b')
    )
    partition = models.CharField(choices=PARTITION_CHOICES)
    # A bunch of other logic...

class Edge(models.Model):
    vertcies = models.ManyToManyField(Vertex, related_name='vertices')


for edge in Edge.objects.all():
   for vertex in edge.vertices.all():
      # some logic

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM