Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
django:如何基于GenericForeignKey';s场?_Django_Generic Foreign Key - Fatal编程技术网

django:如何基于GenericForeignKey';s场?

django:如何基于GenericForeignKey';s场?,django,generic-foreign-key,Django,Generic Foreign Key,我是使用GenericForeignKey的新手,无法在查询语句中使用它。这些表格大致如下所示: class Ticket(models.Model): issue_ct = models.ForeignKey(ContentType, related_name='issue_content_type') issue_id = models.PositiveIntegerField(null=True, blank=True) issue = generic.Generi

我是使用GenericForeignKey的新手,无法在查询语句中使用它。这些表格大致如下所示:

class Ticket(models.Model):
    issue_ct = models.ForeignKey(ContentType, related_name='issue_content_type')
    issue_id = models.PositiveIntegerField(null=True, blank=True)
    issue = generic.GenericForeignKey('issue_ct', 'issue_id')

class Issue(models.Model):
    scan = models.ForeignKey(Scan)
扫描创建一个问题,一个问题生成一些票证,我将该问题作为票证表的外键。现在我有了一个扫描对象,我想查询与此扫描相关的所有票据。我先试了一下:

tickets = Tickets.objects.filter(issue__scan=scan_obj)
这不管用。然后我试了一下:

issue = Issue.objects.get(scan=scan_obj)
content_type = ContentType.objects.get_for_model(Issue)
tickets = Tickets.objects.filter(content_type=content_type, issue=issue)

还是不行。我需要知道如何在django中执行此类查询?谢谢。

您定义的
Ticket.issue
字段将帮助您从
Ticket
实例转到它所附加的
issue
,但它不会让您倒退。您已经接近第二个示例,但是您需要使用
issue\u id
字段-您不能查询
GenericForeignKey
(当您有
票证
实例时,它只会帮助您检索对象)。试试这个:

from django.contrib.contenttypes.models import ContentType

issue = Issue.objects.get(scan=scan_obj)
tickets = Ticket.objects.filter(
    issue_id=issue.id,
    issue_ct=ContentType.objects.get_for_model(issue).id
    )

通过创建与
Ticket
共享
db\u表的第二个模型,可以对
GenericForeignKey
进行过滤。首先将票据分为抽象模型和具体模型

class TicketBase(models.Model):
    issue_ct = models.ForeignKey(ContentType, related_name='issue_content_type')
    issue_id = models.PositiveIntegerField(null=True, blank=True)

    class Meta:
        abstract = True

class Ticket(TicketBase):
    issue = generic.GenericForeignKey('issue_ct', 'issue_id')
然后创建一个模型,该模型也包含子类
TicketBase
。此子类将具有所有相同的字段,但
问题
除外,该问题被定义为
外键
。添加自定义的
管理器
允许将其筛选为单个
内容类型

def subclass_for_content_type(content_type):
    class Meta:
        db_table = Ticket._meta.db_table

    class Manager(models.Manager):
        """ constrain queries to a single content type """
        def get_query_set(self):
            return super(Manager, self).get_query_set().filter(issue_ct=content_type)

    attrs = {
        'related_to': models.ForeignKey(content_type.model_class()),
        '__module__': 'myapp.models',
        'Meta': Meta,
        'objects': Manager()
    }
   return type("Ticket_%s" % content_type.name, (TicketBase,), attrs)
由于此子类不需要同步或迁移,因此可以使用
type()
动态创建它


@girasquid issue_id非常容易混淆,因为它可以指问题中的字段issue_id或问题中的issue字段的id属性,我们可以将它们区分开来吗?