我有一個模型,指向一個通用關係。這可以是聯繫對象或客戶對象。通用關係上的Django過濾器
class Unsubscribe(models.Model):
"""
Notes:
See: http://www.screamingatmyscreen.com/2012/6/django-and-generic-relations/
"""
content_type = models.ForeignKey(ContentType, help_text="Represents the name of the model")
object_id = models.PositiveIntegerField(help_text="stores the object id")
content_object = generic.GenericForeignKey('content_type', 'object_id')
reason = models.CharField(max_length=60)
request_made = models.DateTimeField(auto_now_add=True,
help_text="Shows when object was created.")
class Meta:
ordering = ['-request_made']
我想列出所有未訂閱的退訂客戶和僅爲用戶的聯繫人。
queryset = Unsubscribe.objects.filter()
以上給了我所有退訂的客戶和聯繫人的任何用戶通常我會做解決這個....
queryset = Unsubscribe.objects.filter(user=request.user)
然而,退訂對象沒有用戶,而且均與客戶聯繫人呢。
那麼如何過濾通用關係呢?
我累了,這也查詢集= Unsubscribe.objects.filter(content_object__contact_owner = self.request.user),但它指出content_object不存在,當它確實。 – GrantU
我不認爲'content_obect __...'是有效的字段查找,請嘗試使用我指定的。 – Rohan