django-queryset

Can we do arithmetic using Django Subqueries?

本小妞迷上赌 提交于 2019-12-13 20:18:46
问题 I am wondering if Django's ORM allows us to do aggregate operations on subqueires, and then do arithmetic with the resulting values. What would be the proper way to go about something like this: record = PackingRecord.objects.filter(product=OuterRef('pk')) packed = FifoLink.objects.filter(packing_record__product=OuterRef('pk')) output = obj_set.annotate( in_stock=(Subquery(record.aggregate(Sum('qty'))) - Subquery(packed.aggregate(Sum('sale__qty')))) ).values('id', 'name', 'in_stock') 回答1: You

Django Q Queries & on the same field?

孤者浪人 提交于 2019-12-13 16:15:04
问题 So here are my models: class Event(models.Model): user = models.ForeignKey(User, blank=True, null=True, db_index=True) name = models.CharField(max_length = 200, db_index=True) platform = models.CharField(choices = (("ios", "ios"), ("android", "android")), max_length=50) class User(AbstractUser): email = models.CharField(max_length=50, null=False, blank=False, unique=True) Event is like an analytics event, so it's very possible that I could have multiple events for one user, some with platform

How to add some context to each entry of a django QuerySet

无人久伴 提交于 2019-12-13 14:22:32
问题 I use Django 1.8.4 with Python 3.4 I have a model for tournaments that defines a method which returns a string if a subscription is forbidden. class Tournament(models.Model): name = models.CharField(max_length=200, null=True, blank=True) subscriptions = models.ManyToManyField('ap_users.Profile') is_subscription_open = models.BooleanField(default=True) # ... def why_subscription_impossible(self, request): if not request.user.profile.is_profile_complete(): return 'Your profile is not complete'

Django: search on first letters of individual words within phrase?

China☆狼群 提交于 2019-12-13 14:05:52
问题 I've got a Django model called Author , with a field called name , which isn't split up into lastname/firstname: class Author(models.Model): name = models.CharField(max_length=200, unique=True) For example, I have an Author entry with name 'Donald Knuth'. Now I'd like to query by name, using istartswith at the start of each word within the name field . So I'd like to be able to query with ' Kn ', and get ' Donald Knuth ' back. Something like: authors = Author.objects.filter(name__word_

Django, having Q object filter by function in model

我的未来我决定 提交于 2019-12-13 13:38:10
问题 Inside my Profile model I have the following function: It serves to return the fullname of the user (or a alternative if some data is missing). def full_name(self): first_name = self.user.first_name.strip() if first_name and self.user.last_name: if not self.middle_name: full_name = u'%s %s' % (first_name, self.user.last_name) else: full_name = u'%s %s %s' % (first_name, self.middle_name, self.user.last_name) return full_name.strip() elif first_name: return first_name return self.user.username

Django filter objects

人盡茶涼 提交于 2019-12-13 08:46:15
问题 I am fetching the users who did login today as follows: today_login_count= User.objects.filter(last_login__startswith=timezone.now().date()).count() I want to further filter results and fetch only those users whose username starts with yg_ How can I modify my code? 回答1: Try this count = User.objects.filter(last_login__year=timezone.now().year, last_login__month=timezone.now().month, last_login__day=timezone.now().day, username__startswith='yg_').count() 来源: https://stackoverflow.com/questions

Django filter queryset __in for *every* item in list (2.0)

北城余情 提交于 2019-12-13 07:35:03
问题 I've already read this and this, but they don't solve my problem because they make a final "count" with a comparison to a number that is hard-coded. I want to make a comparison of a number that is the sum of all ingredients of the recipe itself. Let's imagine that I have some ingredients in my fridge, with their id's (= array of id's). I want to see what I can cook with that. I have my models like this: class Ingredient(models.Model): label = models.CharField(max_length=200, null=True, blank

django queryset include more columns in select statement

…衆ロ難τιáo~ 提交于 2019-12-13 07:03:24
问题 I been trying to create a backward relation using queryset and the joining is working fine, accept that its not including the other joined table in the selected columns. Below is my models, queryset and query. str () print class Main(models.Model): slug = models.SlugField() is_active = models.BooleanField(default=True) site = models.ForeignKey(Site) parent = models.ForeignKey('self', blank=True, null=True, limit_choices_to={'parent' : None}) class Meta: unique_together = (("slug", "parent"))

Highly challenging queryset filtering, sorting and annotation (in Django-based app)

梦想的初衷 提交于 2019-12-13 05:29:09
问题 I have a Django-based web-app where users congregate and chat with one another. I've just finished writing a feature whereby users can make their own "chat groups", centered around any topic of interest. These could either be private , or publically visible. My next challenge is showing a list of all existing public groups , paginated, and sorted by the most happening group first . After some deep thinking, I've decided that the most happening group is one which sees the most unique visitors

Combine __icontains lookup with __in lookup in django to get objects that contain part of name in names array

有些话、适合烂在心里 提交于 2019-12-13 04:39:55
问题 Hi My models are as follows class Document(models.Model) id = models.BigIntegerField(primary_key=True) text = models.TextField() class DocumentTags(models.Model): name = model.CharField(max_length=256) documents_for_tag = models.ManyToManyField(Document) Now with this I can get all the tags for a particular document with parent_document = Document.objects.get(id = 1) parent_document_tags = [x.name for x in parent_document.documenttags_set.all()] So the parent_document_tags will have all the