django-queryset

Group by Foreign Key and show related items - Django

こ雲淡風輕ζ 提交于 2019-12-30 01:06:40
问题 I have the following models: class Company(CachedModel): name = models.CharField(max_length=255) class UserExtendedProfile(CachedModel): company = models.ForeignKey(Company) user = models.ForeignKey(User) I basically need to get a list of users ordered by company like this: Company A User 1 User 2 Company B User 3 user 4 I tried a few things, and the closest I could get to is: users = UserExtendedProfile.objects.values('company', 'user').order_by('company') However this would just give me

Django 'objects.filter()' with list?

风格不统一 提交于 2019-12-29 17:44:33
问题 It is possible to limiting QuerySet in this kind of way: creators_list = ['jane', 'tarzan', 'chita'] my_model.objects.filter(creator=creators_list) ??? 回答1: You mean like this? my_model.objects.filter(creator__in=creator_list) Docs: http://docs.djangoproject.com/en/dev/ref/models/querysets/#in EDIT This is now a bit outdated. If you run into problems with the original code, try this: from django.db.models import Q my_filter_qs = Q() for creator in creator_list: my_filter_qs = my_filter_qs | Q

Django 'objects.filter()' with list?

邮差的信 提交于 2019-12-29 17:44:12
问题 It is possible to limiting QuerySet in this kind of way: creators_list = ['jane', 'tarzan', 'chita'] my_model.objects.filter(creator=creators_list) ??? 回答1: You mean like this? my_model.objects.filter(creator__in=creator_list) Docs: http://docs.djangoproject.com/en/dev/ref/models/querysets/#in EDIT This is now a bit outdated. If you run into problems with the original code, try this: from django.db.models import Q my_filter_qs = Q() for creator in creator_list: my_filter_qs = my_filter_qs | Q

Django update queryset with annotation

家住魔仙堡 提交于 2019-12-29 16:25:24
问题 I want to update all rows in queryset by using annotated value. I have a simple models: class Relation(models.Model): rating = models.IntegerField(default=0) class SignRelation(models.Model): relation = models.ForeignKey(Relation, related_name='sign_relations') rating = models.IntegerField(default=0) And I want to awoid this code: for relation in Relation.objects.annotate(total_rating=Sum('sign_relations__rating')): relation.rating = relation.total_rating or 0 relation.save() And do update in

django exclude self from queryset for validation

随声附和 提交于 2019-12-29 07:04:45
问题 I am working with my own clean method to see if some other table already had a field with the same string. This all goes fine as long as i am creating one, but when i try to edit it, it find "itself" and gives back the error. Now am i wondering how can i exclude the instance itself in my clean method def clean_name(self): raw_data = self.cleaned_data['name'] data = raw_data.title() if Country.objects.filter(name=data).exists(): raise forms.ValidationError(("There is already a country with the

Django Order By Date, but have “None” at end?

天大地大妈咪最大 提交于 2019-12-28 05:17:06
问题 I have a model of work orders, with a field for when the work order is required by. To get a list of work orders, with those that are required early, I do this: wo = Work_Order.objects.order_by('dateWORequired') This works nicely, but ONLY if there is actually a value in that field. If there is no required date, then the value is None . Then, the list of work orders has all the None 's at the top, and then the remaining work orders in proper order. How can I get the None 's at the bottom? 回答1

How to create a Django queryset filter comparing two date fields in the same model

喜你入骨 提交于 2019-12-28 05:07:21
问题 Trying to get a query where the Activity record is stale in my Solr Index. I want to check to see if the Activity.updated date in the database is greater than the Activity.added_toSolr_date for the same record. stale_activities_queryset = Activity.objects.filter(updated__gte = self.added_toSolr_date) Model class Activity(models.Model): # Last time entry / metric was updated in the Activity model database updated = models.DateTimeField( verbose_name="CRUD date") # When it was added to Solr

How to create a Django queryset filter comparing two date fields in the same model

流过昼夜 提交于 2019-12-28 05:06:06
问题 Trying to get a query where the Activity record is stale in my Solr Index. I want to check to see if the Activity.updated date in the database is greater than the Activity.added_toSolr_date for the same record. stale_activities_queryset = Activity.objects.filter(updated__gte = self.added_toSolr_date) Model class Activity(models.Model): # Last time entry / metric was updated in the Activity model database updated = models.DateTimeField( verbose_name="CRUD date") # When it was added to Solr

Difference between Django's annotate and aggregate methods?

六月ゝ 毕业季﹏ 提交于 2019-12-28 04:44:30
问题 Django's QuerySet has two methods, annotate and aggregate . The documentation says that: Unlike aggregate(), annotate() is not a terminal clause. The output of the annotate() clause is a QuerySet. Is there any other difference between them? If not, then why does aggregate exist? 回答1: I would focus on the example queries rather than your quote from the documentation. Aggregate calculates values for the entire queryset. Annotate calculates summary values for each item in the queryset.

django - convert a list back to a queryset [duplicate]

非 Y 不嫁゛ 提交于 2019-12-27 17:10:31
问题 This question already has answers here : A QuerySet by aggregate field value (3 answers) Closed 5 years ago . I have a handful of records that I would like to sort based on a computed value. Got the answer over here... like so: sorted(Profile.objects.all(), key=lambda p: p.reputation) on a Profile class like this: class Profile(models.Model): ... @property def reputation(self): ... Unfortunately the generic view is expecting a queryset object and throws an error if I give it a list. Is there