Python / Django - 间接双向m2m关系?

时间:2014-02-25 11:42:03

标签: python django

我有这段代码:

class Reference(models.Model):
  title = models.CharField(max_length=200, verbose_name = _('title'))

  def __unicode__(self):
      return u"%s" % (self.title)

  class Meta:
      verbose_name = _('bibliographic reference')
      verbose_name_plural = _('bibliographic references')


class Relation(models.Model):
  reference = models.ForeignKey(Reference)
  circuit = models.ManyToManyField('Circuit', related_name = 'relation_circuit', verbose_name = _('Circuits'))

  def __unicode__(self):
      return u"%s " %(self.reference)

  class Meta:
      verbose_name = _('relation')
      verbose_name_plural = _('relations')


class Circuit(models.Model):
  name = models.CharField(max_length=200, verbose_name = _('name'))
  reference = models.ManyToManyField(Relation, through=Relation.circuit.through, related_name='relation_circuit', verbose_name = _('Bibliographic References'))

  def __unicode__(self):
      return u"%s" % (self.name)

  class Meta:
      verbose_name = _('circuit')
      verbose_name_plural = _('circuits')

关系在参考中显示为内联。 我需要在我的Circuits和References之间建立一个双向关系,但我不知道如何显示我的所有References而只显示那些有关系的人,因为他们之间存在关系。

任何人都可以帮助我?

非常感谢。

2 个答案:

答案 0 :(得分:0)

这将是我的出发点。

 class Reference(models.Model):
   title = models.CharField(max_length=200)
   relations = models.ManyToManyField('Relation', related_name="reference_relations", null=True, blank=True)

   def __unicode__(self):
      return u"%s" % (self.title,)


class Relation(models.Model):
  reference = models.ForeignKey(Reference)
  circuit = models.ForeignKey('Circuit')

  def __unicode__(self):
      return u"%s <-> %s " %(self.reference, self.circuit)



class Circuit(models.Model):
  name = models.CharField(max_length=200)
  relations = models.ManyToManyField(Relation, related_name="circuit_relations", null=True, blank=True)

  def __unicode__(self):
      return u"%s" % (self.name)

admin.py

from django.contrib import admin
from web.models import *


class RelationInline(admin.TabularInline):
    model = Relation

class CircuitAdmin(admin.ModelAdmin):
    inlines = [
        RelationInline,
    ]

class ReferenceAdmin(admin.ModelAdmin):
    inlines = [
        RelationInline,
    ]

admin.site.register(Reference,ReferenceAdmin)
admin.site.register(Relation)
admin.site.register(Circuit, CircuitAdmin)

当然你可以在没有通过表的情况下做到这一点,但我的偏好是保持关系简单。

答案 1 :(得分:0)

我不确定我是否完全理解你需要什么,但是不能用foreignKey这两个表都不够吗?

这样的东西
 class Reference(models.Model):
    title = models.CharField(max_length=200, verbose_name = _('title'))


 class Circuit(models.Model):
    name = models.CharField(max_length=200, verbose_name = _('name'))


 class Relation(models.Model):
    reference = models.ForeignKey(Reference)
    circuit = models.ForeignKey(Circuit)

然后你可以通过像这样创建一个Relation实例来链接它们:

 circuit = Circuit(name="cool_circuit")
 reference = Reference(title="cool_reference")

 relation = Relation(reference=reference, circuit=circuit)

将所有引用链接到某个电路

也很容易
 circuit = Circuit.objects.get(id=1)

 references_linked_to_circuit_1 = Reference.objects.filter(relation__circuit=circuit)

同样适用于与参考文献相关的所有电路

 reference = Circuit.objects.get(id=1)

 circuits_linked_to_reference_1 = Reference.objects.filter(relation__reference=reference)