django-orm

Django ORM leaves idle connections on Postgres DB

烈酒焚心 提交于 2019-12-08 14:56:24
问题 Recently, my Django app has been crashing frequently due to database connection errors: OperationalError: FATAL: sorry, too many clients already When I go into the app database, I see that indeed there are nearly 100 open connections, all with the same query (executed by the Django ORM) and all in the idle state. I have been manually doing SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE state = 'idle'; but I am perplexed as to why this is happening. Can anyone shed any insight

Can I remove ORDER BY from a Django ORM query?

Deadly 提交于 2019-12-08 14:49:11
问题 It seems like Django by default adds ORDER BY to queries. Can I clear it? from slowstagram.models import InstagramMedia print InstagramMedia.objects.filter().query SELECT `slowstagram_instagrammedia`.`id`, `slowstagram_instagrammedia`.`user_id`, `slowstagram_instagrammedia`.`image_url`, `slowstagram_instagrammedia`.`video_url`, `slowstagram_instagrammedia`.`created_time`, `slowstagram_instagrammedia`.`caption`, `slowstagram_instagrammedia`.`filter`, `slowstagram_instagrammedia`.`link`,

Determine if an attribute is a `DeferredAttribute` in django

南楼画角 提交于 2019-12-08 14:41:56
问题 The Context I have located a rather critical bug in Django Cache Machine that causes it's invalidation logic to lose its mind after a upgrading from Django 1.4 to 1.7. The bug is localized to invocations of only() on models that extend cache machine's CachingMixin . It results in deep recursions that occasionally bust the stack, but otherwise create huge flush_lists that cache machine uses for bi-directional invalidation for models in ForeignKey relationships. class MyModel(CachingMixin): id

How to get similar projects based on tags in Django

为君一笑 提交于 2019-12-08 12:39:28
问题 I have Project and Tag models in my application with a many-to-many relationship between them. On each project's page I want to list 3 additional projects that have the most tags in common with it. How can I perform this query? class Tag(models.Model): name = models.CharField(max_length=300) class Project(models.Model): name = models.CharField(max_length=300) ... tags = models.ManyToManyField(Tag) 回答1: It is possible to pack it all in one line: Project.objects.filter(tags__in=current_project

Multi db and unmanged models - Test case are failing

微笑、不失礼 提交于 2019-12-08 08:15:28
I have mutlidb setup with unmanaged (read-only) models. There's no migrations for these models. I was trying to test the functionality of the view.py. In sqlite3 database these schema of the test table is not creating a causing an issue in failing the test case. In the view.py I have imported the unamanaged (read-only) model is failing. I have followed the link for test Testing against unmanaged models Mentioned the Multi-db setup test_runner.py from django.test.runner import DiscoverRunner class DisableMigrations(object): def __contains__(self, item): return True def __getitem__(self, item):

Django copy/paste Queryset

瘦欲@ 提交于 2019-12-08 05:32:08
问题 What is the Django way to copy and paste Queryset except iterating over records and cloning/saving? E.g. a set of records from table A needs to be selected, some field updated and records inserted back to the original table? A sample use case is adding subscribers from mailing list A to mailing list B. Should it be just a loop iterating over QuerySet and cloning/saving record by record, or there is some method for group operation? 回答1: Django 1.4 has bulk_create method that does his job in 1

Does a Postgresql dump create sequences that start with - or after - the last key?

爱⌒轻易说出口 提交于 2019-12-08 05:06:55
问题 I recently created a SQL dump of a database behind a Django project, and after cleaning the SQL up a little bit was able to restore the DB and all of the data. The problem was the sequences were all mucked up. I tried adding a new user and generated the Python error IntegrityError: duplicate key violates unique constraint . Naturally I figured my SQL dump didn't restart the sequence. But it did: DROP SEQUENCE "auth_user_id_seq" CASCADE; CREATE SEQUENCE "auth_user_id_seq" INCREMENT 1 START 446

Django self join , How to convert this query to ORM query

别等时光非礼了梦想. 提交于 2019-12-08 04:13:37
问题 How can i convert this query to django ORM query. select T.node_id, ht, status, data from ( select id, Max(health_time) as ht, node_id from remote_sense_nodehealth group by node_id ) as T join remote_sense_nodehealth on remote_sense_nodehealth.health_time=T.ht and remote_sense_nodehealth.node_id = T.node_id Actually i want to get all the latest value based on other column value. For example My table is like - c1 | c2 | c3 - - - - - - - x | 1 AM | d1 x | 2 AM | d2 x | 3 AM | d3 y | 1 AM | d4 y

How to filter a queryset based on the result of a method on the model class that returns a boolean?

心不动则不痛 提交于 2019-12-08 03:45:31
问题 As a member function of one of my model classes, I have an is_visible(self, user) method that returns a boolean. As defined, it takes the requesting user (Django User model) as input. I would like to be able to filter querysets based on the response to this method. How can I use this function as a queryset filter? For context, here is my is_visible implementation: def is_visible(self, user): if self.status.status_internal == "open": return True if self.owner == user: return true

Multi db and unmanged models - Test case are failing

白昼怎懂夜的黑 提交于 2019-12-08 03:24:06
问题 I have mutlidb setup with unmanaged (read-only) models. There's no migrations for these models. I was trying to test the functionality of the view.py. In sqlite3 database these schema of the test table is not creating a causing an issue in failing the test case. In the view.py I have imported the unamanaged (read-only) model is failing. I have followed the link for test Testing against unmanaged models Mentioned the Multi-db setup test_runner.py from django.test.runner import DiscoverRunner