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

标签 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')

但是当我从管理员创建关系时,只创建了一个。你能帮我找出问题吗?

最佳答案

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

我怀疑,如果您在 add_balancetransfer() 方法 (import pdb; pdb.set_trace()) 中放置一个断点,您会发现它永远不会被调用 — 创建的关系部分是通过内置的 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
    ...

关于python - 如何在 Django 的 ManyToManyField 上自动创建对称对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11789035/

相关文章:

django - Scrapy 与 DjangoItem 的集成产生错误

django - 多对多关系以及有关该关系的其他数据

python sqlite3更新从变量设置

python - 如何在类中正确创建方法

python - 用 pandas 数据框替换 xlsx 工作表中的数据

python - 通过在每个大写字母前添加一个空格来格式化字符串

python - 如何在 Django 中使具有额外字段对称的递归 ManyToManyField 关系?

python - 列出模板内的ForeignKey关联实例(查询集中的查询集)

python - Django 管理页面错误

python - 使用抽象模型定义字段关系