OneToOneField获取“无法解析关键字”

时间:2015-08-01 19:25:25

标签: mysql django django-models

我已经多次询问过这个问题,但无法根据我的情况调整解决方案。

我使用python manage.py inspectdb从MySQL中的遗留数据库生成模型,并将Pointsofinterestcoordinateslist模型的主键更改为OneToOneField,但无法使查询生效。

models.py

class Parentregionlist(models.Model):
    regionid = models.IntegerField(db_column='RegionID', primary_key=True)   
    parentregionid = models.IntegerField(db_column='ParentRegionID', blank=True, null=True)  # Field name made lowercase.
    ...
    class Meta:
        managed = False
        db_table = 'parentregionlist'

class Pointsofinterestcoordinateslist(models.Model):
    regionid = models.OneToOneField(Parentregionlist, primary_key=True, db_column='RegionID')  # Field name made lowercase.
    regionname = models.CharField(db_column='RegionName', max_length=255, blank=True, null=True)  # Field name made lowercase.
    regionnamelong = models.CharField(db_column='RegionNameLong', primary_key=True, max_length=191)  # Field name made lowercase.
    latitude = models.DecimalField(db_column='Latitude', max_digits=9, decimal_places=6, blank=True, null=True)  # Field name made lowercase.
    longitude = models.DecimalField(db_column='Longitude', max_digits=9, decimal_places=6, blank=True, null=True)  # Field name made lowercase.
    subclassification = models.CharField(db_column='SubClassification', max_length=20, blank=True, null=True)  # Field name made lowercase.
    timestamp = models.DateTimeField(db_column='TimeStamp')  # Field name made lowercase.

    class Meta:
        managed = False
        db_table = 'pointsofinterestcoordinateslist'

views.py

def city(request, name):

attractions = Pointsofinterestcoordinateslist.objects.filter(parentregionlist__parentregionid=2114)

这是我得到的错误:

无法将关键字'parentregionlist'解析为字段。选项包括:纬度,经度,regionid,regionid_id,regionname,regionnamelong,subclassification,timestamp

Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/london

Django Version: 1.8
Python Version: 3.4.3
Installed Applications:
('otezo',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'django.middleware.security.SecurityMiddleware')


Traceback:
File "C:\Python34\lib\site-packages\django\core\handlers\base.py" in get_response
  132.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\projects\otezo\otezo\views.py" in city
  21.     attractions = Pointsofinterestcoordinateslist.objects.get(parentregionlist__parentregionid=2114)
File "C:\Python34\lib\site-packages\django\db\models\manager.py" in manager_method
  127.                 return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Python34\lib\site-packages\django\db\models\query.py" in get
  325.         clone = self.filter(*args, **kwargs)
File "C:\Python34\lib\site-packages\django\db\models\query.py" in filter
  679.         return self._filter_or_exclude(False, *args, **kwargs)
File "C:\Python34\lib\site-packages\django\db\models\query.py" in _filter_or_exclude
  697.             clone.query.add_q(Q(*args, **kwargs))
File "C:\Python34\lib\site-packages\django\db\models\sql\query.py" in add_q
  1301.         clause, require_inner = self._add_q(where_part, self.used_aliases)
File "C:\Python34\lib\site-packages\django\db\models\sql\query.py" in _add_q
  1328.                     current_negated=current_negated, connector=connector, allow_joins=allow_joins)
File "C:\Python34\lib\site-packages\django\db\models\sql\query.py" in build_filter
  1144.         lookups, parts, reffed_aggregate = self.solve_lookup_type(arg)
File "C:\Python34\lib\site-packages\django\db\models\sql\query.py" in solve_lookup_type
  1030.         _, field, _, lookup_parts = self.names_to_path(lookup_splitted, self.get_meta())
File "C:\Python34\lib\site-packages\django\db\models\sql\query.py" in names_to_path
  1386.                                      "Choices are: %s" % (name, ", ".join(available)))

Exception Type: FieldError at /london
Exception Value: Cannot resolve keyword 'parentregionlist' into field. Choices are: latitude, longitude, regionid, regionid_id, regionname, regionnamelong, subclassification, timestamp

任何人都可以帮我解决这个问题吗?非常感谢!

1 个答案:

答案 0 :(得分:2)

从Pointsofinterestcoordinateslist到Parentregionlist(为这些模型提供更好的名称)的字段是regionid,而不是parentregionlist

attractions = Pointsofinterestcoordinateslist.objects.filter(regionid__parentregionid=2114)