django-orm

Django select_related in reverse

非 Y 不嫁゛ 提交于 2021-02-06 10:48:46
问题 I have the following model: class Campaign(models.Model): some_campaign_field = models.CharField() class Position(models.Model): campaign = models.ForeignKey(Campaign) some_position_field = models.CharField() class Trade(models.Model): position = models.ForeignKey(Position) some_trade_field = models.CharField() In other words, I have Campaigns which can have multiple Positions. In turn each position within the campaign can have multiple Trades. Is there an efficient way (ie: minimal database

Django annotate returning unexpected Sum value

﹥>﹥吖頭↗ 提交于 2021-01-29 08:55:53
问题 These are my models: class Consume(models.Model): amount = models.FloatField(default=1) entry_for = models.ForeignKey( Person, on_delete=models.SET_NULL, related_name='consume_entry_for', ) class Purchase(models.Model): amount = models.DecimalField( max_digits=6, decimal_places=2, default=0.00 ) entry_for = models.ForeignKey( Person, on_delete=models.CASCADE, related_name='ledger_entry_for', ) and this is my query: person_wise_total = Person.objects.annotate( total_purchase=Coalesce(Sum(

Django - get latest object in each relation

纵饮孤独 提交于 2021-01-29 05:14:48
问题 Let's assume I have a Product model in my project: class Product(models.Model): price = models.IntegerField() and I want to have some sort of statistics (let's say I want to keep track of how has the price changed over time) for it: class ProductStatistics(models.Model): created = models.DateTimeField(auto_add_now=True) statistics_value = models.IntegerField() product = models.ForeignKey(Product) @classmethod def create_for_product(cls, product_ids): statistics = [] products = Product.objects

Django queryset - column IN() GROUP BY HAVING COUNT DISTINCT

天大地大妈咪最大 提交于 2021-01-28 17:55:51
问题 With the following models: class Post(models.Model): class Meta: db_table = "posts" class Tag(models.Model): tag = models.CharField(max_length=50) class Meta: db_table = "tags" class PostTag(models.Model): postid = models.PositiveIntegerField() tagid = models.PositiveIntegerField() class Meta: unique_together = ("postid", "tagid") db_table = "posttags" To get postids of posts which contain all the tagids given in TAGLIST where TAGLEN is the number of tagids in TAGLIST: SELECT postid FROM

Does Django support JOIN?

余生长醉 提交于 2021-01-27 13:03:46
问题 Does Django ORM support join like INNER JOIN, LEFT JOIN, RIGHT JOIN AND OUTER JOIN? I read the document, and did not found one. For example how can I do this? SELECT * FROM auth_user_groups JOIN test_groups ON auth_user_groups.group_id=test_groups.group_id WHERE test_id='1' AND user_id='2' LIMIT 1 test_group is a Many to many table, without any model class. 回答1: No. Use relational fields and appropriately-crafted queries to perform joins instead. 来源: https://stackoverflow.com/questions

Django ORM raw delete query not deleting records

混江龙づ霸主 提交于 2021-01-27 12:55:02
问题 I am using raw_sql queries for my convenience for keeping my database minimal I am deleting extra records. By this query #d is from a loop and has values res=MyModel.objects.raw("DELETE FROM mydb_mymodel WHERE mydb_mymodel.s_type = '%s' and mydb_mymodel.barcode = '%s' and mydb_mymodel.shopcode = '%s' and mydb_mymodel.date = '%s'" ,[d.s_type,d.barcode,d.shopcode,d.date]) It is not deleting records in database but when I do res.query and run it from postgres console it works! Yes I can use

Django + PostgreSQL: Fill missing dates in a range

℡╲_俬逩灬. 提交于 2021-01-27 05:31:42
问题 I have a table with one of the columns as date . It can have multiple entries for each date. date ..... ----------- ----- 2015-07-20 .. 2015-07-20 .. 2015-07-23 .. 2015-07-24 .. I would like to get data in the following form using Django ORM with PostgreSQL as database backend: date count(date) ----------- ----------- 2015-07-20 2 2015-07-21 0 (missing after aggregation) 2015-07-22 0 (missing after aggregation) 2015-07-23 1 2015-07-24 1 Corresponding PostgreSQL Query: WITH RECURSIVE date_view

Django + PostgreSQL: Fill missing dates in a range

老子叫甜甜 提交于 2021-01-27 05:30:56
问题 I have a table with one of the columns as date . It can have multiple entries for each date. date ..... ----------- ----- 2015-07-20 .. 2015-07-20 .. 2015-07-23 .. 2015-07-24 .. I would like to get data in the following form using Django ORM with PostgreSQL as database backend: date count(date) ----------- ----------- 2015-07-20 2 2015-07-21 0 (missing after aggregation) 2015-07-22 0 (missing after aggregation) 2015-07-23 1 2015-07-24 1 Corresponding PostgreSQL Query: WITH RECURSIVE date_view

Using select_related to get related_name objects

故事扮演 提交于 2021-01-21 10:57:13
问题 I've been searching for a while now for a solution to my problem, but I can't seem to figure this out. So I have two models: class Messages(models.Model): _db = 'somedb' id = models.IntegerField(primary_key=True) text = models.TextField() class MessageTags(models.Model): _db = 'somedb' id = models.IntegerField(primary_key=True) text = models.TextField() tag = models.ForeignKey(Tag) message = models.ForeignKey(Messages, related_name='tags') I am able to query the tags for a given message using

Retrieve the same values ​whose data is there or exists and not the rest.In django

人走茶凉 提交于 2021-01-07 02:56:27
问题 I want to have a data that does not have any empty value in database or in row. I wrote like this in my code. faq = FAQ.objects.values('question','answer','field_id') this the output in my terminal {'question': None, 'answer': None, 'field_id': None} {'question': 'Test question', 'answer': '<p>Testsaddsf description</p>\r\n', 'field_id': 'TestTest'} i don't want None value data. 回答1: You can filter with the __isnull lookup [Django-doc]: faq = FAQ.objects.filter( question __isnull=False ,