Django chained filtering返回Django 1.10中相关字段的无效查找

2024-03-29 10:44:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我在一个运行在ubuntu16.04vm上的django1.10python2.7应用程序中有几个模型,当我试图通过链式关系/过滤获得其中两个模型时,似乎有一种方法失败了。这两种型号是:

 class Crisis(models.Model):
      name = models.CharField(
          max_length=200,
          default=''
      )

      # Time fields

      start_date = models.DateTimeField(
          null=True,
          blank=True
      )

      end_date = models.DateTimeField(
          null=True,
          blank=True
      )

      # Blast zone fields

      radius = models.DecimalField(
          max_digits=14,
          decimal_places=6
      )

      origin = models.PointField(
          srid=4326,
          null=True,
          blank=True
      )

      zone = models.PolygonField(
          srid=4326,
          null=True,
          blank=True
      )

      # Location fields

      country = models.ForeignKey(
          "boundaries.Country",
          verbose_name="country",
          related_name="crises",
          null=True,
          blank=True,
          default=1
      )


      class Meta:

          verbose_name = 'crisis'
          verbose_name_plural = 'crises'

      # Returns the string representation of the model.
      def __unicode__(self):  # __unicode__ on Python 2
          return unicode(self.name)

class Game(models.Model):
      name = models.CharField(
          default='',
          max_length=200,
          null=True,
          blank=True
      )

      number_of_turns = models.IntegerField(
          default=10,
          null=True,
          blank=True
      )

      crisis = models.ForeignKey(
          'games.Crisis',
          related_name='games',
          related_query_name = 'game',
          null = True
      )

      donor = models.ForeignKey(
          'games.Donor',
          related_name='donor',
          blank=True,
          null=True
      )

      description = models.CharField(
          default='',
          max_length=2000,
          null=True,
          blank=True
      )

      def __unicode__(self):  # __unicode__ on Python 2
          return u'{0}'.format(self.name)

应用程序中还使用了其他模型,我可以通过crisis获得game,如下所示:

^{pr2}$

但是当我试图获取特定游戏的所有事务对象时,过滤似乎失败了。

 In [8]: sales = Transaction.objects.filter(buyer__household__scheme__crisis__game_id = gam
     ...: e.id)
  ---------------------------------------------------------------------------
  TypeError                                 Traceback (most recent call last)
  <ipython-input-8-932c009767d2> in <module>()
  ----> 1 sales = Transaction.objects.filter(buyer__household__scheme__crisis__game_id = game.id)

  /vagrant/projectenv/local/lib/python2.7/site-packages/django/db/models/manager.pyc in manager_method(self, *args, **kwargs)
       83         def create_method(name, method):
       84             def manager_method(self, *args, **kwargs):
  ---> 85                 return getattr(self.get_queryset(), name)(*args, **kwargs)
       86             manager_method.__name__ = method.__name__
       87             manager_method.__doc__ = method.__doc__

  /vagrant/projectenv/local/lib/python2.7/site-packages/django/db/models/query.pyc in filter(self, *args, **kwargs)
      794         set.
      795         """
  --> 796         return self._filter_or_exclude(False, *args, **kwargs)
      797 
      798     def exclude(self, *args, **kwargs):

  /vagrant/projectenv/local/lib/python2.7/site-packages/django/db/models/query.pyc in _filter_or_exclude(self, negate, *args, **kwargs)
      812             clone.query.add_q(~Q(*args, **kwargs))
      813         else:
  --> 814             clone.query.add_q(Q(*args, **kwargs))
      815         return clone
      816 

  /vagrant/projectenv/local/lib/python2.7/site-packages/django/db/models/sql/query.pyc in add_q(self, q_object)
     1225         existing_inner = set(
     1226             (a for a in self.alias_map if self.alias_map[a].join_type == INNER))
  -> 1227         clause, _ = self._add_q(q_object, self.used_aliases)
     1228         if clause:
     1229             self.where.add(clause, AND)

  /vagrant/projectenv/local/lib/python2.7/site-packages/django/db/models/sql/query.pyc in _add_q(self, q_object, used_aliases, branch_negated, current_negated, allow_joins, split_subq)
     1251                     child, can_reuse=used_aliases, branch_negated=branch_negated,
     1252                     current_negated=current_negated, connector=connector,
  -> 1253                     allow_joins=allow_joins, split_subq=split_subq,
     1254                 )
     1255                 joinpromoter.add_votes(needed_inner)

  /vagrant/projectenv/local/lib/python2.7/site-packages/django/db/models/sql/query.pyc in build_filter(self, filter_expr, branch_negated, current_negated, can_reuse, connector, allow_joins, split_subq)
     1176                 raise FieldError('Related Field got invalid lookup: {}'.format(lookups[0]))
     1177             assert num_lookups > 0  # Likely a bug in Django if this fails.
  -> 1178             lookup_class = field.get_lookup(lookups[0])
     1179             if len(targets) == 1:
     1180                 lhs = targets[0].get_col(alias, field)

  /vagrant/projectenv/local/lib/python2.7/site-packages/django/db/models/fields/related.pyc in get_lookup(self, lookup_name)
      692             return RelatedIsNull
      693         else:
  --> 694             raise TypeError('Related Field got invalid lookup:     %s' % lookup_name)
      695 
      696     def get_transform(self, *args, **kwargs):

  TypeError: Related Field got invalid lookup: game_id

我不知道为什么相关的姓不起作用,有人能给我建议吗?


Tags: nameinselftruemodelsargsfilterlookup