0
兩個班的models.py
值:如何獲得一個外鍵
class Purchase(models.Model):
book_id = models.CharField(max_length=20, primary_key=True)
storage_id = models.CharField(max_length=20)
def __str__(self):
return self.book_id
class Delivery(models.Model):
book_id = models.CharField(max_length=20, primary_key=True)
storage_id = models.ForeignKey('Purchase', editable=True)
def __str__(self):
return self.book_id
我使用下面的代碼來獲得在交付storage_id
值的形式被保存後:
@receiver(post_save, sender = Delivery)
def removeProducts(sender, instance, **kwargs):
print(instance.storage_id)
問題是,我得到的不是Delivery.storage_id
,它是Purchase.book_id
。如何在不更改__str__
函數的情況下獲得Delivery.storage_id
的值?