如何在Django的ManyToManyField上自动创建对称对象?

时间:2012-08-03 03:51:37

标签: python django django-models django-admin

我想在我的模型上创建对称关系,并在关系中添加一个字段。我遇到this blog(以及this another blog)并按照创建自己模型的步骤进行操作。

class CreditCardIssuer(models.Model):
    name = models.CharField(_('name'), max_length=256)
    transfer_limits = models.ManyToManyField('self', through='Balancetransfer', related_name='no_transfer_allowed+', symmetrical=False, help_text=_('List of issuers to which balance transfer is not allowed.'))

    def add_balancetransfer(self, creditcardissuer, until):
        balance_transfer, _newly_created = Balancetransfer.objects.get_or_create(
            no_transfer_from=self,
            no_transfer_to=creditcardissuer,
            until=until)
        balance_transfer, _newly_created = Balancetransfer.objects.get_or_create(
            no_transfer_from=creditcardissuer,
            no_transfer_to=self,
            until=until)
        return balance_transfer

    def remove_balancetransfer(self, creditcardissuer, until):
        Balancetransfer.objects.filter(
            no_transfer_from=self, 
            no_transfer_to=creditcardissuer,
            until=until).delete()
        Balancetransfer.objects.filter(
            no_transfer_from=self, 
            no_transfer_to=creditcardissuer,
            until=until).delete()
        return

    def get_transfer_limits(self, until):
        return self.transfer_limits.filter(
            no_transfer_to__until=until, 
            no_transfer_to__no_transfer_from=self)


class Balancetransfer(models.Model):
    no_transfer_from = models.ForeignKey('CreditCardIssuer', related_name='no_transfer_from')
    no_transfer_to = models.ForeignKey('CreditCardIssuer', related_name='no_transfer_to')
    until = models.IntegerField(blank=True, null=True, help_text='Minimum card ownership period to allow balance transfer.')

    class Meta:
        unique_together = ('no_transfer_from', 'no_transfer_to')

但是当我从admin创建关系时,只创建了一个。你能帮我解决一下这个问题吗?

1 个答案:

答案 0 :(得分:1)

你说你在Django管理员中有一半(你没有说怎么做,但我会假设它有效,除非你发布代码):剩下的一步是设置一半的缺少的关系。

我怀疑如果你在add_balancetransfer()方法(import pdb; pdb.set_trace())中放置一个断点,你会发现它永远不会被调用 - 所在的关系的一部分 created是通过内置的Django管理员行为完成的。

简而言之,您可能只需要在保存模型时自己调用add_balancetransfer() - 或者更好的是,只添加缺少关系的不同版本。

执行此操作的一种方法是overriding the model's save() method,但这可能有点脆弱。根据您的情况,填写model's post_save signal中缺少的关系可能会更好:

from django.db.models.signals import post_save
from django.dispatch import receiver

from yourapp.models import CreditCardIssuer

@receiver(post_save, sender=CreditCardIssuer)
def add_missing_relationship(sender, **kwargs):
    # Add the other side of what should be a symmetrical relationship
    ...