django-orm

Django: is there a way to count SQL queries from an unit test?

纵然是瞬间 提交于 2020-07-16 16:24:52
问题 I am trying to find out the number of queries executed by a utility function. I have written a unit test for this function and the function is working well. What I would like to do is track the number of SQL queries executed by the function so that I can see if there is any improvement after some refactoring. def do_something_in_the_database(): # Does something in the database # return result class DoSomethingTests(django.test.TestCase): def test_function_returns_correct_values(self): self

Django: is there a way to count SQL queries from an unit test?

痞子三分冷 提交于 2020-07-16 16:21:03
问题 I am trying to find out the number of queries executed by a utility function. I have written a unit test for this function and the function is working well. What I would like to do is track the number of SQL queries executed by the function so that I can see if there is any improvement after some refactoring. def do_something_in_the_database(): # Does something in the database # return result class DoSomethingTests(django.test.TestCase): def test_function_returns_correct_values(self): self

Defining an Abstract model with a ForeignKey to another Abstract model

ぃ、小莉子 提交于 2020-07-07 07:43:20
问题 I'm trying to build two abstract classes called SurveyQuestionBase and SurveyResponseBase that will serve as templates to quickly define new concrete Models for implementing specific surveys on our website. The issue I am having is in enforcing that the SurveyResponseBase model, when made concrete, should define a ForeignKey to a concrete model of SurveyQuestionBase . Django does not allow us to define ForeignKeys to abstract classes so I cannot, for instance, do this: question = models

Defining an Abstract model with a ForeignKey to another Abstract model

淺唱寂寞╮ 提交于 2020-07-07 07:41:22
问题 I'm trying to build two abstract classes called SurveyQuestionBase and SurveyResponseBase that will serve as templates to quickly define new concrete Models for implementing specific surveys on our website. The issue I am having is in enforcing that the SurveyResponseBase model, when made concrete, should define a ForeignKey to a concrete model of SurveyQuestionBase . Django does not allow us to define ForeignKeys to abstract classes so I cannot, for instance, do this: question = models

django - How to copy actual image file from one model to another?

♀尐吖头ヾ 提交于 2020-06-25 22:49:10
问题 I want to copy images from one model to another within the project. Suppose these are my models: class BackgroundImage(models.Model): user = models.ForeignKey(User) image = models.ImageField(upload_to=get_upload_file_name) caption = models.CharField(max_length=200) pub_date = models.DateTimeField(default=datetime.now) class ProfilePicture(models.Model): user = models.ForeignKey(User) image = models.ImageField(upload_to=get_upload_file_name) caption = models.CharField(max_length=200) pub_date

django - How to copy actual image file from one model to another?

落爺英雄遲暮 提交于 2020-06-25 22:49:06
问题 I want to copy images from one model to another within the project. Suppose these are my models: class BackgroundImage(models.Model): user = models.ForeignKey(User) image = models.ImageField(upload_to=get_upload_file_name) caption = models.CharField(max_length=200) pub_date = models.DateTimeField(default=datetime.now) class ProfilePicture(models.Model): user = models.ForeignKey(User) image = models.ImageField(upload_to=get_upload_file_name) caption = models.CharField(max_length=200) pub_date

Django model method - create_or_update

久未见 提交于 2020-05-25 06:09:20
问题 Similar to get_or_create, I would like to be able to update_or_create in Django. Until now, I have using an approaching similar to how @Daniel Roseman does it here. However, I'd like to do this more succinctly as a model method. This snippet is quite old and I was wondering if there is a better way to do this in more recent version of Django. 回答1: See QuerySet.update_or_create (new in Django 1.7dev) 回答2: There is update_or_create , eg:: obj, created = Person.objects.update_or_create( first

How to get only latest record on filter of foreign key django

南笙酒味 提交于 2020-05-12 04:49:09
问题 I have a table like this Event table | id | status | date |order(FK)| | 1 | Planned | 05-02-2015 | 1 | | 2 | Delivered | 04-02-2015 | 2 | | 3 | Packed | 03-02-2015 | 3 | | 4 | Return | 06-02-2015 | 1 | I want output like this | id | status | date |order(FK)| | 2 | Delivered | 04-02-2015 | 2 | | 3 | Packed | 03-02-2015 | 3 | | 4 | Return | 06-02-2015 | 1 | I tried with query = Event.objects.annotate(order_num=Max('date')) but didn't got the expected result. How can i achieve this output 回答1:

Django MySql Fulltext search works but not on tests

瘦欲@ 提交于 2020-04-30 07:16:06
问题 I used this SO question to enable full text search on a mysql db in Django application. # models.py class CaseSnapshot(BaseModel): text = models.TextField(blank=True) class Search(models.Lookup): lookup_name = "search" def as_mysql(self, compiler, connection): lhs, lhs_params = self.process_lhs(compiler, connection) rhs, rhs_params = self.process_rhs(compiler, connection) params = lhs_params + rhs_params return f"MATCH (%s) AGAINST (%s IN BOOLEAN MODE)" % (lhs, rhs), params models.TextField

Django Queryset __in with None value in list

帅比萌擦擦* 提交于 2020-04-12 16:11:32
问题 a = M.objects.filter(f__in=[None, 1]) a.query.__str__() u'SELECT * FROM "app_m" WHERE "app_m"."f" IN (None, 1)' dont you think that would be IN (NULL, 1) ? like: a = M.objects.filter(f=None) a.query.__str__() u'SELECT * FROM "app_m" WHERE "app_m"."f" IS NULL' Is this a default SQL behavior, django bug or I am missing something with f__in= ? thank you in advance! 回答1: a = M.objects.filter(Q(f__isnull=True) | Q(f__in=['1',...])) 回答2: It seems to be and old bug in Django (https://code