【问题标题】:Django: How to unlink generic relation?Django:如何取消链接通用关系?
【发布时间】:2013-02-23 14:58:19
【问题描述】:

如何取消链接泛型关系?

我只想取消 Note 和 Customer 的链接。

models.py

class Note(models.Model):
    contents = models.TextField()

    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    content_object = generic.GenericForeignKey('content_type', 'object_id')


class Customer(models.Model):
    name = models.CharField(max_length=50, unique=True,)
    notes = generic.GenericRelation(Note, null=True)

>>> cs=Customer.objects.get(pk=1)
>>> cs.notes.all()[0].delete()

但是cs.notes.all()[0] 被完全删除了。

我不想完全删除。我只是想取消链接...

我该怎么办?

【问题讨论】:

    标签: django django-1.4


    【解决方案1】:

    唯一的“链接”存在于Note 对象的content_typeobject_id 引用Customer 实例这一事实。所以,改变这些,链接就会消失。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-18
      • 2021-07-16
      相关资源
      最近更新 更多