问题标签 [django-mysql]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
2 回答
821 浏览

mysql - 将 Django 项目连接到远程 Mysql 数据库

我正在Django Project我的本地主机上工作,我想使用一个远程的MySQL Database.

我的本地主机 IP 是:172.30.10.XX

我的 MySQL 远程服务器是:172.30.10.XX

在我的Django settings.py文件中,我写道:

我的数据库名称是:DatasystemsEC

但是,当我运行 : 时python manage.py migrate,我收到此错误:

在带有 phpmyadmin 的 MySQL 上,我有:

在此处输入图像描述

我假设我需要配置一个新用户才能注册我的 MacOSX localhost?

所以我创建了一个osx具有所有授予权限的新用户。但它仍然不起作用。

如果你能帮助我,谢谢

0 投票
1 回答
1209 浏览

mysql - 在 MySQL 和 Django 中使用 Count 连接 2 个表

第 1 部分:MySQL

从 MySQL 数据库我如何加入以下两个计数

这些查询返回两个单独的表,但我应该能够在 Activity.user_id = Comment.author_id 上加入它们。

第一个查询给出:

第二个查询给出:

我要返回的是:

我还需要 0 值用于仅出现在 1 个表中的活动和评论(NULL 可以正常工作)。

第 2 部分:Django

是否可以在不使用原始查询的情况下在 Django 中的单个查询中执行此操作?

将 Python 中的 2 个单独查询与 2 个 Django 查询集或 SQL 连接起来会更快吗?

我有存储在不同模型中的用户的活动和评论:

具有返回必要数据的条件的两个单独查询是:

显然可以通过几种不同的方式来执行此操作,我正在尝试找到最快的方法来执行此操作,因为我正在处理 Activity 表中的超过 550 万条记录和 Comment 表中的超过 790k 条记录。

0 投票
1 回答
570 浏览

python - 如何限制用户在 django 中保存数据库?

我如何限制用户将对象保存在数据库中

我只想将foo限制为每个用户 10 个条目

我使用mysql作为后端

0 投票
1 回答
1056 浏览

mysql - 第1146章

我正在使用 Django 1.11 来制作应用程序“cnfs”,并且我正在使用 MYSQL 数据库。我经常遇到这个问题,当我输入以下代码时,我会收到这样的错误:

系统检查发现了一些问题: 警告:?:(mysql.W002)MySQL 严格模式未设置为数据库连接“默认”提示:MySQL 的严格模式通过升级警告修复了 MySQL 中的许多数据完整性问题,例如插入时数据截断进入错误。强烈建议您激活它。请参阅:https ://docs.djangoproject.com/en/1.11/ref/databases/#mysql-sql-mode 要执行的操作:应用所有迁移:admin、auth、cnfs、contenttypes、sites 运行迁移:没有要应用的迁移。回溯(最近一次调用):文件“manage.py”,第 22 行,在 execute_from_command_line(sys.argv) 文件“/home/ubuntu/.local/lib/python2.7/site-packages/django/core/management /初始化.py”,第 363 行,在 execute_from_command_line utility.execute () 文件“/home/ubuntu/.local/lib/python2.7/site-packages/django/core/management/init.py”,第 355 行,在执行 self.fetch_command(subcommand).run_from_argv(self.argv) 文件“/home/ubuntu/.local/lib/python2.7/site-packages/django/core/management/base. py”,第 283 行,在 run_from_argv self.execute(*args, **cmd_options) 文件“/home/ubuntu/.local/lib/python2.7/site-packages/django/core/management/base.py”中,第 330 行,在执行输出 = self.handle(*args, **options) 文件“/home/ubuntu/.local/lib/python2.7/site-packages/django/core/management/commands/migrate.py”,第 227 行,在句柄 self.verbosity、self.interactive、connection.alias、apps=post_migrate_apps、plan=plan、文件“/home/ubuntu /.local/lib/python2.7/site-packages/django/core/management/sql.py”,第 53 行,在 emit_post_migrate_signal **kwargs 文件“/home/ubuntu/.local/lib/python2.7/site -packages/django/dispatch/dispatcher.py”,第 193 行,在 self._live_receivers(sender) 文件“/home/ubuntu/.local/lib/python2.7/site-packages/django/contrib/sites/management.py”,第 20 行,如果不是 Site.objects.using(using).exists(),则在 create_default_site 中:文件“/home/ubuntu/ .local/lib/python2.7/site-packages/django/db/models/query.py”,第 670 行,存在返回 self.query.has_results(using=self.db) 文件“/home/ubuntu/. local/lib/python2.7/site-packages/django/db/models/sql/query.py",第 517 行,在 has_results 中返回 compiler.has_results() 文件"/home/ubuntu/.local/lib/python2.7/site-packages/django/db/models/sql/compiler.py”,第 845 行,在 has_results 返回 bool(self.execute_sql(SINGLE)) 文件“/home /ubuntu/.local/lib/python2.7/site-packages/django/db/models/sql/compiler.py",第 886 行,在 execute_sql 中引发 original_exception django.db.utils.ProgrammingError: (1146, "Table ' cnfs.django_site' 不存在")在 has_results 返回 bool(self.execute_sql(SINGLE)) 文件“/home/ubuntu/.local/lib/python2.7/site-packages/django/db/models/sql/compiler.py”,第 886 行,在 execute_sql引发 original_exception django.db.utils.ProgrammingError:(1146,“表 'cnfs.django_site' 不存在”)在 has_results 返回 bool(self.execute_sql(SINGLE)) 文件“/home/ubuntu/.local/lib/python2.7/site-packages/django/db/models/sql/compiler.py”,第 886 行,在 execute_sql引发 original_exception django.db.utils.ProgrammingError:(1146,“表 'cnfs.django_site' 不存在”)在 execute_sql 中引发 original_exception django.db.utils.ProgrammingError:(1146,“表 'cnfs.django_site' 不存在”)在 execute_sql 中引发 original_exception django.db.utils.ProgrammingError:(1146,“表 'cnfs.django_site' 不存在”)

我确实在这上面花了几天时间,当我使用默认的 django 数据库引擎 sqlite3 时,我没有遇到这个问题。我试过这样做:$python manage.py migrate sites

我的 SITE_ID 设置设置为 1。我尝试从 INSTALLED_APPS 设置中删除“django.contrib.sites”,但这会引发此错误:

内部服务器错误:/ Traceback(最近一次调用最后一次):文件“/home/ubuntu/.local/lib/python2.7/site-packages/django/core/handlers/exception.py”,第 41 行,在内部响应中= get_response(request) 文件“/home/ubuntu/.local/lib/python2.7/site-packages/django/utils/deprecation.py”,第 138 行,调用中 response = self.process_request(request) 文件“/home/ubuntu/.local/lib/python2.7/site-packages/subdomains/middleware.py”,第 62 行,在 process_request super(SubdomainURLRoutingMiddleware, self).process_request(request )文件“/home/ubuntu/.local/lib/python2.7/site-packages/subdomains/middleware.py”,第38行,在process_request(self.get_domain_for_request(request),request.get_host())文件“ /home/ubuntu/.local/lib/python2.7/site-packages/subdomains/middleware.py”,第 31 行,在 get_domain_for_request 返回 get_domain() 文件“/home/ubuntu/.local/lib/python2.7/site-packages/subdomains/utils.py”,第 12 行,在 current_site_domain 从 django.contrib.sites.models 导入站点文件“ /home/ubuntu/.local/lib/python2.7/site-packages/django/contrib/sites/models.py”,第 84 行,在类站点(models.Model)中:文件“/home/ubuntu/.local /lib/python2.7/site-packages/django/db/models/base.py”,第 118 行,在新的 “INSTALLED_APPS”。% (module, name) RuntimeError: Model class django.contrib.sites.models.Site 没有声明明确的 app_label 并且不在 INSTALLED_APPS 的应用程序中。

任何建议将不胜感激。

谢谢。

0 投票
1 回答
299 浏览

mysql - djagno过滤器正则表达式在python重新工作时不起作用

我正在尝试在 django 中为正则表达式进行 iregex 匹配

通过使用单词“The Canton”作为名称,但它不返回任何值,但在python re.search中使用它时它的工作

我正在使用 Mysql 5.7,任何人都知道如何解决这个问题

0 投票
2 回答
1128 浏览

python - Django - 跨查询集的 DatetimeField 的时间聚合

(使用 django 1.11.2、python 2.7.10、mysql 5.7.18)

如果我们想象一个简单的模型:

运行类似于以下内容的最优雅(和最快)的方式是什么:

但这将能够为查询集的所有成员提取一天中的平均时间。就像是:

是否可以直接在数据库上执行此操作?即,无需为每个查询集成员运行长循环客户端?

编辑:

可能有一个解决方案,沿着这些思路,使用原始 SQL:

性能方面,这在笔记本电脑上运行约 23k 行的时间为 0.015 秒,未进行优化等。假设这可以产生准确/正确的结果,并且由于时间只是次要因素,我可以使用它吗?

0 投票
2 回答
1169 浏览

django - 从一开始就使用 MySql 和虚拟环境设置 Django 项目

我是 django 新手,所以我需要从 django 项目开始。我需要有关如何在 Linux 上使用 MySql 数据库和虚拟环境开始 django 项目的信息。

0 投票
2 回答
1013 浏览

django - 过滤 JSONField 数组

我有一个书籍模型

我有一些记录:

我想过滤掉带有“技术”或“商业”标签的书籍

我尝试使用 contains、contained_by、has_key、has_any_keys 但没有运气。结果总是空的。

更新

这是我的错!我发现了问题,JSONField 区分大小写。

保存在 DB 中的值["TECH", "BUSINESS"]不是["Tech", "Business"].

现在问题变成了How to search in a case-insensitive manner?

0 投票
1 回答
825 浏览

mysql - Django/MySQL: Making non-unique field unique fails even if field values are unique

I currently have this:

I need to make the field committee_xml_id unique, i.e. make it so:

I have also tried making it so:

Alas, the result is the same.

Having run ./manage.py makemigrations and subsequently, ./manage.py migrate, the problem is this:

At first glance, it seems like the problem is that there is already non-unique data in the table, but the problem is precisely that there isn't. There are only 45 rows in the table, and the field committee_xml_id contains only unique entries.

The following query gives no results, as expected when there are no duplicates:

For rigor, here is the same query without the HAVING-condition, showing clearly that there are indeed no duplicates:

Result is:

Any help greatly appreciated.

0 投票
2 回答
1883 浏览

django - Django configuring different databases

I have 2 databases of interest, the basic one and the development one:

In my django file mysite/mysite/settings.py, my databases are declared this way:

The allowed hosts is:

I start the server on the port 8006 which I use for developing:

And here I modify the production database. I can switch to the dev database replacing the default database name:

And it works fine, the server is interacting with the projectsdb_dev database. However I would like to keep both databases available in the settings file and I saw tutorials setting it up this way:

Now when I open the web page on xxx.xx.xxx.xx:8006, I get this error:

I don't know if it's relevant but I also have this table:

How can I run the server specifying the correct database I want?