Filter Models based on content type django 1.7

好久不见. 提交于 2019-12-24 13:27:34

问题


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

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

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

I get this error.

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


回答1:


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)


来源:https://stackoverflow.com/questions/26753580/filter-models-based-on-content-type-django-1-7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!