gpt4 book ai didi

python - django 使用外键连接

转载 作者:行者123 更新时间:2023-11-28 23:39:36 24 4
gpt4 key购买 nike

我是 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com