根据内容类型django 1.7过滤模型

时间:2014-11-05 09:29:07

标签: django django-models django-1.7 django-contenttypes

我的models.py看起来像

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列表来自"活动"模型。

在python shell中,当我做

之类的事情时
>>> event = Event.objects.get(pk = 1)
>>> event.content_type
<ContentType: one time event>

从上面的代码我可以知道&#34;主键是一次性事件的事件的内容类型&#34;但是当我做的时候

>>> 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'
>>> 

我收到此错误。

早些时候Filter 2 models with ContentType in Django提出了一个类似的问题,我尝试了同样的问题但是使用这个解决方案我得到了空列表

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

我该怎么办? 提前致谢

1 个答案:

答案 0 :(得分:4)

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

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