我想在我的模型上创建一个对称关系,并在关系中添加一个字段。我遇到了这个博客(以及另一个博客),并按照步骤创建了我自己的模型。
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')
但是当我从管理员创建关系时,只创建了一个。你能帮我找出问题所在吗?