django-queryset

Using list comprehension instead of for loop when working with Django QuerySets

浪尽此生 提交于 2019-12-23 23:16:29
问题 I'm trying to tweak an application that is suffering in speed department. Because of that, I've started converting all the for-loop statements to list comprehensions when possible. Currently, I'm working on a function that needs to iterate through a dictionary of Django querysets. The old code uses for-loop statement to iterate through this and it works fine. My code that uses list comprehension returns django querysets instead of my model object. Here is the code: def get_children(parent): #

Using list comprehension instead of for loop when working with Django QuerySets

天大地大妈咪最大 提交于 2019-12-23 22:35:19
问题 I'm trying to tweak an application that is suffering in speed department. Because of that, I've started converting all the for-loop statements to list comprehensions when possible. Currently, I'm working on a function that needs to iterate through a dictionary of Django querysets. The old code uses for-loop statement to iterate through this and it works fine. My code that uses list comprehension returns django querysets instead of my model object. Here is the code: def get_children(parent): #

How to order django rest framework queryset from serializer field?

我与影子孤独终老i 提交于 2019-12-23 22:24:56
问题 How can I order a Django QuerySet from a serializer field? Since the field is too complicated, I couldn't order the QuerySet using annotate and I cannot store the value in the model as well. Edit: Serializer class DrinkListModelSerializer(serializers.ModelSerializer): count_need = serializers.SerializerMethodField() url = drink_detail_url class Meta: model = Drink fields = [ 'name', 'count_need', 'thumbnail', 'url' ] def get_count_need(self, obj): drink_ingredient_qs = obj.ingredients.all()

Simple Djanqo Query generating confusing Queryset results

大憨熊 提交于 2019-12-23 19:52:49
问题 [ Update : software versions Python 2.7.2, Django 1.3.1] Can anyone explain this console code? FinishingStep has a ForeignKey to a quote object, but that's not really relevant. >>> fins = FinishingStep.objects.filter(quote=jq) >>> fins [<FinishingStep: Tabbing>, <FinishingStep: Collator>] So far so good, we have returned a QuerySet with two objects. But now the confusion. Both objects now appear to be the same: >>> fins[0] <FinishingStep: Collator> >>> fins[1] <FinishingStep: Collator>

Is there a way to make a query respect the order of the inputted parameters?

旧街凉风 提交于 2019-12-23 19:41:49
问题 (Please let me know if this is completely absurd, that is probably a reason why I haven't found anything on this.) This story has two models Ranking and Artist , Ranking is generically related to Artist (object_id, content_type... the whole shebang). I have a list of objects returned by Ranking.objects.values_list() ordered by a certain field (in my case score ). So obviously, if I want to display a list of artists that were ranked, I'd want them in the same order. I've tried different

Override Django User Manager to only return active users in queries

六月ゝ 毕业季﹏ 提交于 2019-12-23 13:58:53
问题 Need a way for calls to User.objects.filter/get to only return User objects with is_active set to True. I tried to define a custom Manager and monkey patch it onto the User model, like this: class CustomUserManager(UserManager): def get_query_set(self): return super(CustomUserManager, self).get_query_set(). filter(is_active=True) User.objects = CustomUserManager() User.objects_all = UserManager() But, when I try to make a call to User.objects.get(), I get: AttributeError: 'NoneType' object

Django not like statement

流过昼夜 提交于 2019-12-23 12:08:09
问题 how to use not like in django queries Model.objects.filter(keywords not like "null" or "undefined") select * from model where keywords not like "%undefined%" or keywords not like "%null%"; 回答1: use the exclude function and Q objects Model.objects.exclude(Q(keyword__contains='undefined') | Q(keyword__contains='null')) 来源: https://stackoverflow.com/questions/4193097/django-not-like-statement

django rest framework nested modelserializer

核能气质少年 提交于 2019-12-23 09:24:10
问题 this is realted to my other question django-rest-framework, multitable model inheritance, ModelSerializers and nested serializers In django rest framework we can define nested model serializer like so class OtherModelSerializer(serializer.ModelSerializer): mybasemodel_set = MyBaseModelSerializer(many=True) class Meta: model = OtherModel when we create an OtherModelSerializer, the MyBaseModelSerializer is instantiated before __init__ is run. I believe this is the case because if I override the

Django annotate on BooleanField

ぐ巨炮叔叔 提交于 2019-12-23 08:47:27
问题 I have the following models: class Foo(models.Model): pass class Bar(models.Model): foo = models.ForeignKey(Foo) is_successful = models.BooleanField() I would like to get all foo objects with an annotation if all of the bar objects associated with foo object have is_successful as True So far my queryset is: foos = Foo.objects.all().annotate(all_successful=Min('bar__is_successful')) The idea for the all_successful annotation is that if the minimum value of all is_successful rows is 1, then all

django - aggregate json field specific keys and order by the aggregation

一笑奈何 提交于 2019-12-23 03:40:11
问题 I have a model with the a field data of type JSONField from django.contrib.postgres.fields . The json structure is like so: {'aa': 1, 'bb': 2, 'cc': 4} I want to aggregate the sums of the aa and cc keys - so in this case, it will be 5. Also - i cannot promise that either aa or cc will be in the json. Is this possible? If so - i want to order by the aggregated data. Example: id: 1, data = {'aa': 1, 'bb': 2, 'cc':4} id: 2, data = {'aa': 3, 'bb': 2} id: 3, data = {'cc': 7} id: 4, data = {'bb': 7