0

这是我的模型

我检查了这个链接,但它没有解决我的问题

User, Business 与 Deposit 有通用关系

class Deposit(BaseModel):
    amount = models.IntegerField(default=0, blank=False)
    # set up generic foreign keys
    content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
    object_id = models.PositiveIntegerField()
    depositor = GenericForeignKey()

    def __str__(self):
        return "%s %s made by %s" % (self.amount, self.deposit_type, self.depositor)

class User(AbstractUser, BaseModelMixin):
   
    first_name = models.CharField(max_length=256)
    last_name = models.CharField(max_length=256)
    employers = models.ManyToManyField(Business, through="Employment")
    address = models.ForeignKey(Address, null=True, blank=True, on_delete=models.CASCADE)
    email = models.EmailField("Email Address", blank=True, unique=True)
    deposits = GenericRelation("Deposit", related_query_name="user")

class Business(BaseModel):
    name = models.CharField(max_length=255)
    tax_id = models.CharField(max_length=255, blank=True, default="")
    contact_email = models.CharField(max_length=255)
    contact_first_name = models.CharField(max_length=255)
    contact_last_name = models.CharField(max_length=255)
    contact_phone = models.CharField(max_length=255, blank=True, default="")
    description = models.TextField(blank=True, default="")
    address = models.ForeignKey(Address, null=True, blank=True, on_delete=models.CASCADE)
    deposits = GenericRelation("Deposit", related_query_name="business")

Deposit当我使用 Python shell创建对象时

business = Business.objects.get(pk=1)
deposits= Deposit.objects.create(business=business, amount=50)

我收到这个错误


TypeError: Direct assignment to the reverse side of a related set is prohibited. Use business.set() instead.

如何解决这个问题?

4

0 回答 0