django-queryset

django queryset filter foreignkey

喜你入骨 提交于 2019-12-13 03:56:15
问题 I'm having problems trying to use the queryset filter with my models. It is a control for posts in groups. This is my code: class Post(models.Model): title = models.CharField(max_length=120) content = models.TextField() class Group(models.Model): title = models.CharField(max_length=200) url = models.URLField(unique=True) class Control(models.Model): published = models.DateField(auto_now=False, auto_now_add=False) group = models.ForeignKey(Group, on_delete=models.CASCADE) post = models

two QuerySets - merge/join on common foreign key of third model

无人久伴 提交于 2019-12-13 02:48:22
问题 I am now querying - independent of each other - the Models Price and Duration (scroll down for the model definition) through several filters. Finally, I want to merge Price and Duration Querysets so that one price is associated with the specific duration. Example: Price - in QuerySet _prices: id | provider | servicelevel | price | currency 1 | 1 | 1 | 10 | 1 2 | 1 | 1 | 20 | 2 3 | 2 | 2 | 15 | 1 Duration - in QuerySet _duration: id | servicelevel | country_in | country_out | duration |

How to query this (snow flake) data schema in django

谁说我不能喝 提交于 2019-12-13 02:03:12
问题 I want to query a snow flake data schema in django and cannot get it right. model.py class City(models.Model): city_name = models.CharField(max_length=30, default='') class CityTranslations(models.Model): language = models.CharField(max_length=2, default='--') city_name_trans = models.CharField(max_length=30, default='') city = models.ForeignKey(City) class Doctor(models.Model): doctor_name = models.CharField(max_length=30, default='') city = models.ForeignKey(City) class DoctorTranslations

Django created_at__gt=self.request.user.last_login workinly only on users already signed in.

强颜欢笑 提交于 2019-12-12 23:12:51
问题 Intro: I have a 3 models user , post , group . User is able to make posts however each post has to belong to a group. Users have to choose from the existing groups for their posts. Users cannot add, delete, update group's. Furthermore: Users can become a member of groups and when they click on a certain group. They see all the posts in that group. What I want When Users come on the home page they see posts that were added since the last time they logged in My Models class Post(models.Model):

Django annotate queryset with intersection count

本秂侑毒 提交于 2019-12-12 19:36:42
问题 Djangonauts, I need to tap your brains. In a nutshell, I have the following three models: class Location(models.Model): name = models.CharField(max_length=100) class Profile(models.Model): locations_of_interest = models.ManyToManyField(Location) class Question(models.Model): locations = models.ManyToManyField(Location) I want to find all Profiles which locations of interest intersect with the locations specified for a certain question. That’s easy: question = Question.objects.first() matching

django queryset aggregation count counting wrong thing

大兔子大兔子 提交于 2019-12-12 18:31:25
问题 This is a continuation question from: Django queryset get distinct column values with respect to other column My Problem: Using aggregate count in Django counts the wrong thing in the queryset, or as far as I can see something that is not even in my queryset. What I did I used: queryset.order_by('col1', 'col2').distinct('col1', 'col2').values('col2') to get the values of col2 of a model where all the rows have a distinct pair of values in (col1, col2) . There is an example in the link above.

Django complex query to fetch data from groupby and having clause

穿精又带淫゛_ 提交于 2019-12-12 18:23:38
问题 I want to execute group by clause on MyUser table having CNT.Status exactly one the raw query would be like below. SELECT user_id from user_table GROUP BY user_id HAVING COUNT(status)=1 I tried various options using Django model API but its adding "id" too in group by clause. 回答1: It seems you are looking for filtering on annotations: qs = ( MyUser .objects .annotate(num_status=Count('status')) .filter(num_status__gt=1) ) Notice: I supose that status is a 1:N related model. 来源: https:/

How do i split a very long string into a list of shorter strings in python

二次信任 提交于 2019-12-12 15:15:35
问题 In my current django project I have a model that stores very long strings (can be 5000-10000 or even more characters per DB entry) and then i need to split them when a user is calling the record (it really need to be in one record in the DB). What i need is it to return a list (queryset? depends if in the "SQL" part or getting all the list as is and doing the parsing in the view) of shorter strings (100 - 500 characters per sting in the list i return to the template). I couldn't find anywhere

django - queryset in modelForm

北战南征 提交于 2019-12-12 14:21:12
问题 I need to filter Food model by datetime in forms.py , but I do not know how to do it. Could anyone help me? models.py class Food(models.Model): class Meta: verbose_name = "Food" verbose_name_plural = "Foods" def __unicode__(self): return self.food_name food_name = models.CharField(verbose_name="Food Name", max_length=50) serve_date = models.DateTimeField(verbose_name="Serve Date") forms.py class Reserve(forms.ModelForm): food_name = forms.ModelChoiceField( queryset=Food.objects.all(), widget

How to compute average of an aggregate in django

末鹿安然 提交于 2019-12-12 09:55:34
问题 If I have an aggregate, can I get the average of the values in the query, without computing it in python memory? from django.db.models import F, Sum, FloatField, Avg Model.objects.filter(...)\ .values('id')\ .annotate(subtotal=Sum(...math here...), output_field=FloatField())\ .annotate(total=Avg(F('subtotal'))) #this line throws a FieldError Is there any way to get the Avg of the subtotal values in the query? It gives me an error that I'm not allowed to compute Avg on an aggregate (" subtotal