django-queryset

Group by 2 fields combination and then order by the sum of each group, multiple annotations django

╄→гoц情女王★ 提交于 2019-12-08 12:52:35
问题 I was trying to get top products ordered by their total margin sum from invoices as: Filter invoices by given store_id Group by product_name And the get the Sum of gross_margin of each group Finally order them by Sum of gross_margin (high to low) Wrong previous code: high_margin = StoreInvoiceBreakup.objects \ .filter(store_invoice__store_id=store_id) \ .annotate(product_count=Count('product_name')) \ .annotate(gross_margin_sum=Sum('gross_margin')) \ .order_by('-gross_margin_sum') \ .values

django queryset excluding entries in second model

China☆狼群 提交于 2019-12-08 11:52:31
问题 I'm making a little vocabulary-quiz app, and the basic model for a word is this: class Word(models.Model): id = models.AutoField(primary_key=True) word = models.CharField(max_length=80) id_image = models.ForeignKey(Image) def __unicode__(self): return self.word class Meta: db_table = u'word' The model for words I'm currently quizzing myself on is this: class WordToWorkOn(models.Model): id = models.AutoField(primary_key=True) id_student = models.ForeignKey(Student) id_word = models.ForeignKey

How to custom sort a Django queryset

爷,独闯天下 提交于 2019-12-08 10:32:37
问题 Given a data model with Title strings, say: class DVD(models.Model): title = models.CharField(max_length=100) class DVDAdmin(admin.ModelAdmin): ordering = ('title',) sample_titles = {"A Fish Called Wanda", "The Good, the Bad, and the Unsorted", "A River Runs Upstream", "The Incredibles",} I want to generate a queryset sorted by title, but considering the title as minus any leading words that are in a list, such as ("a", "an", "the",). So "The Incredibles" would sort before "A River Runs

Django - getting object by its related fields' IDs

旧时模样 提交于 2019-12-08 09:26:07
问题 Let's assume we have following models: from django.db import models class Foo(models.Model): name = models.CharField(max_length=50) class Bar(models.Model): name = models.CharField(max_length=50) class Zomg(models.Model): foo = models.ForeignKey(Foo) bar = models.ForeignKey(Bar) In Zomg model foo and bar fields serve as composite key, that is, for any pair of ( foo , bar ) there is only one Zomg object. In my project I need to update thousands of Zomg records from time to time, so efficiency

Recursive QuerySet with django

点点圈 提交于 2019-12-08 07:29:05
问题 I have this model referencing itself to allow building a tree: class PartCategory(models.Model): parent = models.ForeignKey('PartCategory', on_delete=models.DO_NOTHING, null=True, default=None, blank=True) name = models.TextField() I now have an SQL query to get one element and all of its child in one select (in this example the element with id=64): WITH RECURSIVE under_partcategory(id,name, parent_id,level) AS ( select api_partcategory.id,api_partcategory.name,api_partcategory.parent_id,0

Django - ImageField files not showing up?

狂风中的少年 提交于 2019-12-08 06:08:53
问题 This is my models.py: class UserImages(models.Model): user = models.ForeignKey(User) photo = models.ImageField(upload_to=get_file_path) and this is my view which uploads images: def uploadImageView(request): if request.method == 'POST': form = UploadImageForm(request.POST, request.FILES) if form.is_valid(): instance = form.save(commit=False) instance.user = request.user instance.save() return redirect('/') else: form = UploadImageForm() return render(request, 'image.html', {'form': form}) and

How to build complex django query as a string

穿精又带淫゛_ 提交于 2019-12-08 00:05:11
问题 I am dynamically generating a query string with multiple parameters. I am trying to include the object names ('nut', 'jam') in my string. The query has to be an "OR" query. My code is below and I get the error shown below. The solutions here, here, and here did not work for me. from viewer.models import Model1 from django.db.models import Q list1 = [ {'nut' : 'peanut', 'jam' : 'blueberry'}, {'nut' : 'almond', 'jam' : 'strawberry'} ] query_string = "" for x in list1: if len(query_string) == 0:

Custom properties in a query

蓝咒 提交于 2019-12-07 22:59:47
问题 Given the simplified example below, how would I access my custom "current_status" property within a queryset? Is it even possible? At the moment, I want to list the all the current Events and show the current status. I can get the property to display in a template ok, but I can't order the queryset by it. Alternatively, would I need to create a custom manager with some kind of nested "if" statement in the 'Select'? class Event(models.Model): .... date_registered = models.DateField(null=True,

Avoiding n+1 queries to query model plus all ForeignKey associations?

放肆的年华 提交于 2019-12-07 19:26:08
问题 I have two models: call them questions and answers: class FacetQuestion(models.Model): the_question = models.CharField(max_length=50) class FacetAnswer(models.Model): question = models.ForeignKey(FacetQuestion) display_order = models.SmallIntegerField() the_answer = models.CharField(max_length=1024) I'd like to present all the questions and answers in one list, with the questions and answers ordered per my choosing: Q1 A1 A2 A3 Q2 A10 A9 A4 Without creating n+1 database queries or creating

Django Admin, can't groupe by: Exception Value: 'dict' object has no attribute '_meta'

主宰稳场 提交于 2019-12-07 17:24:56
问题 I have this model which maps to a postgresql view class AppModel(models.Model): nbr = models.BigIntegerField(blank=True, null=True) region = models.ForeignKey(AppWilaya,blank=True, null=True) date_preorder = models.DateField(blank=True, null=True) id = models.IntegerField(primary_key=True,blank=True, db_column='dummy_id') What I want to achieve is to sum "nbr" by "region" , so: class AppModelAdmin(admin.ModelAdmin): .... def queryset(self, request): qs = super(AppModelAdmin, self).get