基于内容类型的过滤器模型django 1.7 [英] Filter Models based on content type django 1.7

查看:165
本文介绍了基于内容类型的过滤器模型django 1.7的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的models.py看起来像

My models.py looks like

class OneTimeEvent(models.Model):
    title = models.CharField(max_length = 160)
    location = models.CharField(max_length = 200, blank = True)
    event_date = models.DateTimeField('event date',blank = True, null = True)
    price = models.IntegerField(max_length = 20, blank = True, null = True)
    seats = models.IntegerField(max_length = 20, blank = True, null = True)
    abstract = models.TextField()
    event_plan = models.TextField()
    available_seats = models.IntegerField(max_length = 20, blank = True, null = True)
    booked_seats = models.IntegerField(max_length = 20, blank = True, null = True)
    byt_url = models.CharField(max_length = 160, blank = True)
    tags = models.TextField()

class RecurringEvent(models.Model):
    title = models.CharField(max_length = 160)
    location = models.CharField(max_length = 200, blank = True)
    price = models.IntegerField(max_length = 10)

class Event(models.Model):
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    event_type = generic.GenericForeignKey('content_type', 'object_id')

来自事件模型的OneTimeEvent的ID列表。

I want to get the list of IDs of "OneTimeEvent" from "Event" model.

在python shell中,当我正在做类似

In python shell when I'm doing something like

>>> event = Event.objects.get(pk = 1)
>>> event.content_type
<ContentType: one time event>

从上面的代码我可以知道主键是一次事件的事件的内容类型但是当我做了

From above code I can know that "content type of the event whose primary key is one time event" but when I do

>>> Event.objects.filter(content_type = "one time event")
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/manager.py", line 92, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 691, in filter
    return self._filter_or_exclude(False, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 709, in _filter_or_exclude
    clone.query.add_q(Q(*args, **kwargs))
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/query.py", line 1287, in add_q
    clause, require_inner = self._add_q(where_part, self.used_aliases)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/query.py", line 1314, in _add_q
    current_negated=current_negated, connector=connector)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/query.py", line 1181, in build_filter
    lookups, value)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/related.py", line 1545, in get_lookup_constraint
    lookup_class(Col(alias, target, source), val), AND)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/lookups.py", line 82, in __init__
    self.rhs = self.get_prep_lookup()
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/lookups.py", line 85, in get_prep_lookup
    return self.lhs.output_field.get_prep_lookup(self.lookup_name, self.rhs)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py", line 646, in get_prep_lookup
    return self.get_prep_value(value)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py", line 915, in get_prep_value
    return int(value)
ValueError: invalid literal for int() with base 10: 'one time event'
>>> 

我收到此错误。

在Django中使用ContentType过滤2个模型,I尝试相同但是用这个解决方案我得到空列表

One similar question is asked earlier Filter 2 models with ContentType in Django, I tried same but with this solution I get the empty list

>>> Event.objects.filter(content_type__model = 'OneTimeEvent')
[]

做。
提前感谢

What should I do. Thanks in advance

推荐答案

content_type 是外键到ContentType模型。该模型有各种管理方法,其中包括 get_for_model ,它需要一个模型类(注意,而不是一个字符串),并返回相关的ContentType对象。所以:

content_type is a foreign key to the ContentType model. That model has various manager methods, among them get_for_model, which takes a model class (note, not a string) and returns the relevant ContentType object. So:

ct = ContentType.objects.get_for_model(OneTimeEvent)
Event.objects.filter(content_type=ct)

这篇关于基于内容类型的过滤器模型django 1.7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆