- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
'default': {},
'portaldb': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'med_portal',
'OPTIONS': {
'init_command': "SET sql_mode='STRICT_TRANS_TABLES'"
},
'USER': '****',
'PASSWORD': '',
'HOST': '***.***.**.*',
'PORT': '****',
},
'smgpr': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'supplies',
'OPTIONS': {
'init_command': "SET sql_mode='STRICT_TRANS_TABLES'"
},
'USER': '****',
'PASSWORD': '',
'HOST': '***.***.
**.*',
'PORT': '****',
},
#LocalRouter.py
class DBLocalRouter(object):
def db_for_read(self, model, **hints):
app_list = ('auth', 'admin', 'contenttypes', 'sessions', 'messages', 'staticfiles', 'Test', 'Admin', 'Charges', 'Products', 'Patients', 'Relationships', 'apis', 'corsheaders', 'djoser', 'django_filters', 'users', 'rest_framework', 'debug_toolbar')
if model._meta.app_label in app_list:
return 'smgpr'
return None
def db_for_write(self, model, **hints):
app_list = ('auth', 'admin', 'contenttypes', 'sessions', 'messages', 'staticfiles', 'Test', 'Admin', 'Charges', 'Products', 'Patients', 'Relationships', 'apis', 'corsheaders', 'djoser', 'django_filters', 'users', 'rest_framework', 'debug_toolbar')
if model._meta.app_label in app_list:
return 'smgpr'
return None
def allow_relation(self, obj1, obj2, **hints):
app_list = ('auth', 'admin', 'contenttypes', 'sessions', 'messages', 'staticfiles', 'Test', 'Admin', 'Charges', 'Products', 'Patients', 'Relationships', 'apis', 'corsheaders', 'djoser', 'django_filters', 'users', 'rest_framework', 'debug_toolbar')
if obj1._meta.app_label in app_list and obj2._meta.app_label in app_list:
return True
return None
def allow_migrate(self, db, app_label, model=None, **hints):
app_list = ('auth', 'admin', 'contenttypes', 'sessions', 'messages', 'staticfiles', 'Test', 'Admin', 'Charges', 'Products', 'Patients', 'Relationships', 'apis', 'corsheaders', 'djoser', 'django_filters', 'users', 'rest_framework', 'debug_toolbar')
if app_label in app_list:
return db == 'smgpr'
return None
#GlobalRouter.py
class DBGlobalRouter(object):
def db_for_read(self, model, **hints):
"""
Attempts to read portal models go to portal.
"""
app_list = ('portal', 'Test',)
if model._meta.app_label in app_list:
return 'portaldb'
return None
def db_for_write(self, model, **hints):
app_list = ('portal', 'Test',)
if model._meta.app_label in app_list:
return 'portaldb'
return None
def allow_relation(self, obj1, obj2, **hints):
app_list = ('portal', 'Test',)
if obj1._meta.app_label in app_list and obj2._meta.app_label in app_list:
return True
return True
def allow_migrate(self, db, app_label, model=None, **hints):
app_list = ('portal', 'Test',)
if app_label in app_list:
return db == 'portaldb'
return None
class User(AbstractBaseUser):
username = models.CharField(max_length=255, unique=True)
email = models.EmailField(max_length=255, unique=True)
full_name = models.CharField(max_length=255, blank=True, null=True)
custparent = models.ManyToManyField('portal.Custparent', related_name='facility', through='Relation')
active = models.BooleanField(default=True)
staff = models.BooleanField(default=False)
admin = models.BooleanField(default=False)
jwt_secret = models.UUIDField(default=uuid.uuid4)
created_at = models.DateTimeField(auto_now_add=True)
class Relation(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.DO_NOTHING)
custparent = models.ForeignKey('portal.Custparent', related_name='profile', on_delete=models.DO_NOTHING)
class Meta:
db_table= 'relation'
unique_together = ('user', 'custparent')
pbd_name = settings.DATABASES['portaldb']['NAME']
class Custparent(models.Model):
custparent_id = models.BigAutoField(primary_key=True)
name = models.CharField(max_length=255, blank=True, null=True)
comm_queue = models.CharField(max_length=128, blank=True, null=True)
email = models.CharField(db_column='EMail', max_length=255, blank=True, null=True) # Field name made lowercase.
invoice_email = models.CharField(db_column='Invoice_EMail', max_length=128, blank=True, null=True) # Field name made lowercase.
portal_id = models.IntegerField(blank=True, null=True)
sentupdate = models.DateTimeField(db_column='SentUpdate', blank=True, null=True) # Field name made lowercase.
webaddress = models.CharField(max_length=255, blank=True, null=True)
webext = models.CharField(max_length=3, blank=True, null=True)
passcode = models.CharField(db_column='PassCode', max_length=128, blank=True, null=True) # Field name made lowercase.
autoapprove = models.IntegerField(db_column='AutoApprove', blank=True, null=True) # Field name made lowercase.
testaccount = models.IntegerField(db_column='TestAccount', blank=True, null=True) # Field name made lowercase.
privateemail = models.IntegerField(db_column='PrivateEmail', blank=True, null=True) # Field name made lowercase.
forcemanponum = models.IntegerField(db_column='ForceManPONum', blank=True, null=True) # Field name made lowercase.
procuretopay = models.IntegerField(db_column='ProcureToPay', blank=True, null=True) # Field name made lowercase.
medassets_member = models.IntegerField(db_column='MedAssets_Member', blank=True, null=True) # Field name made lowercase.
medassets_facnum = models.IntegerField(db_column='MedAssets_FacNum', blank=True, null=True) # Field name made lowercase.
hideglorder = models.IntegerField(db_column='HideGLOrder', blank=True, null=True) # Field name made lowercase.
searchall = models.IntegerField(db_column='SearchAll', blank=True, null=True) # Field name made lowercase.
accountlabel = models.CharField(db_column='AccountLabel', max_length=50, blank=True, null=True) # Field name made lowercase.
inhouselabel = models.CharField(db_column='InHouseLabel', max_length=50, blank=True, null=True) # Field name made lowercase.
privatelabellogo = models.CharField(db_column='PrivateLabelLogo', max_length=120, blank=True, null=True) # Field name made lowercase.
hin = models.CharField(db_column='HIN', max_length=9, blank=True, null=True) # Field name made lowercase.
gln = models.CharField(db_column='GLN', max_length=13, blank=True, null=True) # Field name made lowercase.
shiptoattn = models.CharField(db_column='ShiptoAttn', max_length=64, blank=True, null=True) # Field name made lowercase.
shiptoaddress = models.CharField(db_column='ShiptoAddress', max_length=64, blank=True, null=True) # Field name made lowercase.
shiptocity = models.CharField(db_column='ShiptoCity', max_length=64, blank=True, null=True) # Field name made lowercase.
shiptostateprov = models.CharField(db_column='ShiptoStateProv', max_length=24, blank=True, null=True) # Field name made lowercase.
shiptopostalcode = models.CharField(db_column='ShiptoPostalCode', max_length=24, blank=True, null=True) # Field name made lowercase.
shiptophone = models.CharField(db_column='ShiptoPhone', max_length=24, blank=True, null=True) # Field name made lowercase.
shiptofax = models.CharField(db_column='ShiptoFax', max_length=24, blank=True, null=True) # Field name made lowercase.
multivendor = models.IntegerField(db_column='MultiVendor', blank=True, null=True) # Field name made lowercase.
lockformulary = models.IntegerField(db_column='LockFormulary', blank=True, null=True) # Field name made lowercase.
active = models.IntegerField(db_column='Active', blank=True, null=True) # Field name made lowercase.
autoadded = models.IntegerField(db_column='AutoAdded', blank=True, null=True) # Field name made lowercase.
allowpatientedit = models.IntegerField(db_column='AllowPatientEdit', blank=True, null=True) # Field name made lowercase.
displaymaxonorders = models.IntegerField(db_column='DisplayMaxOnOrders', blank=True, null=True) # Field name made lowercase.
class Meta:
managed = False
# db_table = 'custparent'
db_table = '`%s`.`custparent`' % pbd_name
def __str__(self):
return f'{str(self.custparent_id)} - {self.name}'
使用管理中的关系表创建对象时,一切正常。然而,每当我尝试使用 DjangoRestFramework 查看可浏览 api 中的 User 对象时。我收到此错误:django.db.utils.ProgrammingError:(1146,“表'med_portal.relation'不存在”)
Traceback (most recent call last):
File "/home/iclark/.local/share/virtualenvs/backend-2BnCgv4_/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/home/iclark/.local/share/virtualenvs/backend-2BnCgv4_/lib/python3.6/site-packages/django/core/handlers/base.py", line 117, in _get_response
response = middleware_method(request, callback, callback_args, callback_kwargs)
File "/home/iclark/.local/share/virtualenvs/backend-2BnCgv4_/lib/python3.6/site-packages/debug_toolbar/middleware.py", line 85, in process_view
response = panel.process_view(request, view_func, view_args, view_kwargs)
File "/home/iclark/.local/share/virtualenvs/backend-2BnCgv4_/lib/python3.6/site-packages/debug_toolbar/panels/profiling.py", line 160, in process_view
return self.profiler.runcall(view_func, *args, **view_kwargs)
File "/usr/lib/python3.6/cProfile.py", line 109, in runcall
return func(*args, **kw)
File "/home/iclark/.local/share/virtualenvs/backend-2BnCgv4_/lib/python3.6/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "/home/iclark/.local/share/virtualenvs/backend-2BnCgv4_/lib/python3.6/site-packages/rest_framework/viewsets.py", line 116, in view
return self.dispatch(request, *args, **kwargs)
File "/home/iclark/.local/share/virtualenvs/backend-2BnCgv4_/lib/python3.6/site-packages/rest_framework/views.py", line 495, in dispatch
response = self.handle_exception(exc)
File "/home/iclark/.local/share/virtualenvs/backend-2BnCgv4_/lib/python3.6/site-packages/rest_framework/views.py", line 455, in handle_exception
self.raise_uncaught_exception(exc)
File "/home/iclark/.local/share/virtualenvs/backend-2BnCgv4_/lib/python3.6/site-packages/rest_framework/views.py", line 492, in dispatch
response = handler(request, *args, **kwargs)
File "/home/iclark/.local/share/virtualenvs/backend-2BnCgv4_/lib/python3.6/site-packages/rest_framework/mixins.py", line 45, in list
return self.get_paginated_response(serializer.data)
File "/home/iclark/.local/share/virtualenvs/backend-2BnCgv4_/lib/python3.6/site-packages/rest_framework/serializers.py", line 765, in data
ret = super(ListSerializer, self).data
File "/home/iclark/.local/share/virtualenvs/backend-2BnCgv4_/lib/python3.6/site-packages/rest_framework/serializers.py", line 262, in data
self._data = self.to_representation(self.instance)
File "/home/iclark/.local/share/virtualenvs/backend-2BnCgv4_/lib/python3.6/site-packages/rest_framework/serializers.py", line 683, in to_representation
self.child.to_representation(item) for item in iterable
File "/home/iclark/.local/share/virtualenvs/backend-2BnCgv4_/lib/python3.6/site-packages/rest_framework/serializers.py", line 683, in <listcomp>
self.child.to_representation(item) for item in iterable
File "/home/iclark/.local/share/virtualenvs/backend-2BnCgv4_/lib/python3.6/site-packages/rest_framework/serializers.py", line 527, in to_representation
ret[field.field_name] = field.to_representation(attribute)
File "/home/iclark/.local/share/virtualenvs/backend-2BnCgv4_/lib/python3.6/site-packages/rest_framework/relations.py", line 542, in to_representation
for value in iterable
File "/home/iclark/.local/share/virtualenvs/backend-2BnCgv4_/lib/python3.6/site-packages/django/db/models/query.py", line 268, in __iter__
self._fetch_all()
File "/home/iclark/.local/share/virtualenvs/backend-2BnCgv4_/lib/python3.6/site-packages/django/db/models/query.py", line 1186, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "/home/iclark/.local/share/virtualenvs/backend-2BnCgv4_/lib/python3.6/site-packages/django/db/models/query.py", line 54, in __iter__
results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
File "/home/iclark/.local/share/virtualenvs/backend-2BnCgv4_/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1065, in execute_sql
cursor.execute(sql, params)
File "/home/iclark/.local/share/virtualenvs/backend-2BnCgv4_/lib/python3.6/site-packages/debug_toolbar/panels/sql/tracking.py", line 186, in execute
return self._record(self.cursor.execute, sql, params)
File "/home/iclark/.local/share/virtualenvs/backend-2BnCgv4_/lib/python3.6/site-packages/debug_toolbar/panels/sql/tracking.py", line 124, in _record
return method(sql, params)
File "/home/iclark/.local/share/virtualenvs/backend-2BnCgv4_/lib/python3.6/site-packages/django/db/backends/utils.py", line 100, in execute
return super().execute(sql, params)
File "/home/iclark/.local/share/virtualenvs/backend-2BnCgv4_/lib/python3.6/site-packages/django/db/backends/utils.py", line 68, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/home/iclark/.local/share/virtualenvs/backend-2BnCgv4_/lib/python3.6/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/home/iclark/.local/share/virtualenvs/backend-2BnCgv4_/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "/home/iclark/.local/share/virtualenvs/backend-2BnCgv4_/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/home/iclark/.local/share/virtualenvs/backend-2BnCgv4_/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "/home/iclark/.local/share/virtualenvs/backend-2BnCgv4_/lib/python3.6/site-packages/django/db/backends/mysql/base.py", line 71, in execute
return self.cursor.execute(query, args)
File "/home/iclark/.local/share/virtualenvs/backend-2BnCgv4_/lib/python3.6/site-packages/MySQLdb/cursors.py", line 250, in execute
self.errorhandler(self, exc, value)
File "/home/iclark/.local/share/virtualenvs/backend-2BnCgv4_/lib/python3.6/site-packages/MySQLdb/connections.py", line 50, in defaulterrorhandler
raise errorvalue
File "/home/iclark/.local/share/virtualenvs/backend-2BnCgv4_/lib/python3.6/site-packages/MySQLdb/cursors.py", line 247, in execute
res = self._query(query)
File "/home/iclark/.local/share/virtualenvs/backend-2BnCgv4_/lib/python3.6/site-packages/MySQLdb/cursors.py", line 411, in _query
rowcount = self._do_query(q)
File "/home/iclark/.local/share/virtualenvs/backend-2BnCgv4_/lib/python3.6/site-packages/MySQLdb/cursors.py", line 374, in _do_query
db.query(q)
File "/home/iclark/.local/share/virtualenvs/backend-2BnCgv4_/lib/python3.6/site-packages/MySQLdb/connections.py", line 277, in query
_mysql.connection.query(self, query)
django.db.utils.ProgrammingError: (1146, "Table 'med_portal.relation' doesn't exist")
最佳答案
假设您有这些过于简单的结构:
app1.models
from django.db import models
class A(models.Model):
z = models.TextField()
app2.models
from django.db import models
class B(models.Model):
aref = models.ForeignKey(to='app1.A', on_delete=models.CASCADE)
app1.app1_router
class MyAppRouter:
def db_for_read(self, model, **hints):
if model._meta.app_label in ('app1', 'test'):
return 'app1_db'
return 'default'
def db_for_write(self, model, **hints):
if model._meta.app_label in ('app1', 'test'):
return 'app1_db'
return None
def allow_relation(self, obj1, obj2, **hints):
if obj1._meta.app_label in ('app1', 'test') or obj2._meta.app_label in ('app1', 'test'):
return True
return None
def allow_migrate(self, db, app_label, model_name=None, **hints):
if app_label in ('app1', 'test'):
return db == 'app1_db'
return None
数据库的这些设置:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
},
'app1_db': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'app1.sqlite3'),
}
}
DATABASE_ROUTERS = ["app1.app1_router.MyAppRouter",]
现在显然我们有两个数据库
显然,我们有一个从 app2.B
(位于 default
数据库中)到 app1 的
(位于 aref =foreignKey
。 Aapp1_db
数据库中)。
我们显然会将数据库迁移为:
./manage.py migrate
./manage.py migrate --database=app1_db
覆盖两个数据库。
现在假设我们要创建两个对象并将它们链接到这些模型:
a1 = A.objects.create()
b1 = B.objects.create(aref=a1)
将会发生的是,a1
实例将很容易创建,因为我们的 MyRouter
路由器将处理数据库写入,并将指导 SQL INSERT
根据需要将 code> 添加到 app1_db
中,但是当我们尝试编写 B
实例时,尤其是当我们尝试链接到 时,问题就出现了app1_a
模型,该模型在 default
数据库中不存在(由于 aref=a1
导致发生)。
我猜当您尝试通过 rest_framework
访问它时发生的情况基本上是相同的。
事情是 Django 一次在一个数据库上运行,就我而言,有两个选项:
ForeignKey
实现 like in this article ,与 this implementation 关于python - django.db.utils.ProgrammingError : (1146, "Table ' med_portal.Custparent'不存在”),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53890563/
我正在处理一组标记为 160 个组的 173k 点。我想通过合并最接近的(到 9 或 10 个组)来减少组/集群的数量。我搜索过 sklearn 或类似的库,但没有成功。 我猜它只是通过 knn 聚类
我有一个扁平数字列表,这些数字逻辑上以 3 为一组,其中每个三元组是 (number, __ignored, flag[0 or 1]),例如: [7,56,1, 8,0,0, 2,0,0, 6,1,
我正在使用 pipenv 来管理我的包。我想编写一个 python 脚本来调用另一个使用不同虚拟环境(VE)的 python 脚本。 如何运行使用 VE1 的 python 脚本 1 并调用另一个 p
假设我有一个文件 script.py 位于 path = "foo/bar/script.py"。我正在寻找一种在 Python 中通过函数 execute_script() 从我的主要 Python
这听起来像是谜语或笑话,但实际上我还没有找到这个问题的答案。 问题到底是什么? 我想运行 2 个脚本。在第一个脚本中,我调用另一个脚本,但我希望它们继续并行,而不是在两个单独的线程中。主要是我不希望第
我有一个带有 python 2.5.5 的软件。我想发送一个命令,该命令将在 python 2.7.5 中启动一个脚本,然后继续执行该脚本。 我试过用 #!python2.7.5 和http://re
我在 python 命令行(使用 python 2.7)中,并尝试运行 Python 脚本。我的操作系统是 Windows 7。我已将我的目录设置为包含我所有脚本的文件夹,使用: os.chdir("
剧透:部分解决(见最后)。 以下是使用 Python 嵌入的代码示例: #include int main(int argc, char** argv) { Py_SetPythonHome
假设我有以下列表,对应于及时的股票价格: prices = [1, 3, 7, 10, 9, 8, 5, 3, 6, 8, 12, 9, 6, 10, 13, 8, 4, 11] 我想确定以下总体上最
所以我试图在选择某个单选按钮时更改此框架的背景。 我的框架位于一个类中,并且单选按钮的功能位于该类之外。 (这样我就可以在所有其他框架上调用它们。) 问题是每当我选择单选按钮时都会出现以下错误: co
我正在尝试将字符串与 python 中的正则表达式进行比较,如下所示, #!/usr/bin/env python3 import re str1 = "Expecting property name
考虑以下原型(prototype) Boost.Python 模块,该模块从单独的 C++ 头文件中引入类“D”。 /* file: a/b.cpp */ BOOST_PYTHON_MODULE(c)
如何编写一个程序来“识别函数调用的行号?” python 检查模块提供了定位行号的选项,但是, def di(): return inspect.currentframe().f_back.f_l
我已经使用 macports 安装了 Python 2.7,并且由于我的 $PATH 变量,这就是我输入 $ python 时得到的变量。然而,virtualenv 默认使用 Python 2.6,除
我只想问如何加快 python 上的 re.search 速度。 我有一个很长的字符串行,长度为 176861(即带有一些符号的字母数字字符),我使用此函数测试了该行以进行研究: def getExe
list1= [u'%app%%General%%Council%', u'%people%', u'%people%%Regional%%Council%%Mandate%', u'%ppp%%Ge
这个问题在这里已经有了答案: Is it Pythonic to use list comprehensions for just side effects? (7 个答案) 关闭 4 个月前。 告
我想用 Python 将两个列表组合成一个列表,方法如下: a = [1,1,1,2,2,2,3,3,3,3] b= ["Sun", "is", "bright", "June","and" ,"Ju
我正在运行带有最新 Boost 发行版 (1.55.0) 的 Mac OS X 10.8.4 (Darwin 12.4.0)。我正在按照说明 here构建包含在我的发行版中的教程 Boost-Pyth
学习 Python,我正在尝试制作一个没有任何第 3 方库的网络抓取工具,这样过程对我来说并没有简化,而且我知道我在做什么。我浏览了一些在线资源,但所有这些都让我对某些事情感到困惑。 html 看起来
我是一名优秀的程序员,十分优秀!