作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 django 2.7 的新手。我正在尝试使用外键约束运行连接查询。我有两个表 table1 和 table2,它们具有以下属性:
Model.py
-:
class table1(models.Model):
abcid = models.IntegerField(db_column='abcid', primary_key=True) # Field name made lowercase.
abcName = models.CharField(db_column='abcName', max_length=50, blank=True, null=True) # Field name made lowercase.
abcyear = models.IntegerField(db_column='abcYear', blank=True, null=True)
class table2(models.Model):
abcid = models.ForeignKey('table1', models.DO_NOTHING, db_column='abcid')
xyzname = models.CharField(db_column='xyzName', max_length=150, blank=True, null=True) # Field name made lowercase.
xyztype = models.CharField(db_column='xyzType', max_length=150, blank=True, null=True) # Field name made lowercase.
我想获取 xyzname 包含“pineapple”的所有记录。我需要的列是 -abcid,abcname,abcyear,xyzname
到目前为止我已经尝试过的是:
table1.objects.filter(table2__xyzname__icontains = 'pineapple')
table2.objects.filter(xyzname__icontains= 'pineapple').table1_set.all()
请帮助。错误-:
Request Method: GET
Request URL: http://127.0.0.1:8000/abc/trade/
Django Version: 1.9
Python Version: 2.7.3
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'abc_act']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'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']
Traceback:
File "/home/deep/workspace/src/abcJournal/abcjournal/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
149. response = self.process_exception_by_middleware(e, request)
File "/home/deep/workspace/src/abcJournal/abcjournal/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
147. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/deep/workspace/src/abcNew/abc_journal/abcjournal/abcsoft/abc_act/views.py" in abcactname
18. data = serializers.serialize('json',abcactDS)
File "/home/deep/workspace/src/abcJournal/abcjournal/local/lib/python2.7/site-packages/django/core/serializers/__init__.py" in serialize
129. s.serialize(queryset, **options)
File "/home/deep/workspace/src/abcJournal/abcjournal/local/lib/python2.7/site-packages/django/core/serializers/base.py" in serialize
79. for count, obj in enumerate(queryset, start=1):
File "/home/deep/workspace/src/abcJournal/abcjournal/local/lib/python2.7/site-packages/django/db/models/query.py" in __iter__
258. self._fetch_all()
File "/home/deep/workspace/src/abcJournal/abcjournal/local/lib/python2.7/site-packages/django/db/models/query.py" in _fetch_all
1074. self._result_cache = list(self.iterator())
File "/home/deep/workspace/src/abcJournal/abcjournal/local/lib/python2.7/site-packages/django/db/models/query.py" in __iter__
52. results = compiler.execute_sql()
File "/home/deep/workspace/src/abcJournal/abcjournal/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py" in execute_sql
852. cursor.execute(sql, params)
File "/home/deep/workspace/src/abcJournal/abcjournal/local/lib/python2.7/site-packages/django/db/backends/utils.py" in execute
79. return super(CursorDebugWrapper, self).execute(sql, params)
File "/home/deep/workspace/src/abcJournal/abcjournal/local/lib/python2.7/site-packages/django/db/backends/utils.py" in execute
64. return self.cursor.execute(sql, params)
File "/home/deep/workspace/src/abcJournal/abcjournal/local/lib/python2.7/site-packages/django/db/utils.py" in __exit__
95. six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/home/deep/workspace/src/abcJournal/abcjournal/local/lib/python2.7/site-packages/django/db/backends/utils.py" in execute
64. return self.cursor.execute(sql, params)
File "/home/deep/workspace/src/abcJournal/abcjournal/local/lib/python2.7/site-packages/django/db/backends/mysql/base.py" in execute
112. return self.cursor.execute(query, args)
File "/home/deep/workspace/src/abcJournal/abcjournal/local/lib/python2.7/site-packages/MySQLdb/cursors.py" in execute
205. self.errorhandler(self, exc, value)
File "/home/deep/workspace/src/abcJournal/abcjournal/local/lib/python2.7/site-packages/MySQLdb/connections.py" in defaulterrorhandler
36. raise errorclass, errorvalue
Exception Type: OperationalError at /abc/trade/
Exception Value: (1054, "Unknown column 'table2.id' in 'field list'")
最佳答案
table2.objects.filter(xyzname__icontains='pineapple') \
.select_related('abcid__abcid',
'abcid__abcName',
'abcid__abcyear')
select_related
django doc .
关于python - django 使用外键连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34751871/
我有 2 个表,它们的第一列都是 PRIMARY,这也是自动递增的。第一个表有 67 个条目,从 1 到 67,第二个表有 48 个条目。它们都有相同的列。我想从 Table2 中获取内容并将它们插入
我是一名优秀的程序员,十分优秀!