django-orm

Django Query: Return average monthly spend per user

Django Query: Return average monthly spend per user Question: Given a Django model which stores user transactions, how can I create a query that returns the average monthly spend for each user? My current solution queries all the transactions , iterates the result and calculates the averages for each user using a dictionary. I am …

Total answers: 1

django: get() returned more than one Freelancers — it returned 3

django: get() returned more than one Freelancers — it returned 3 Question: i am trying to assign a freelancer to a particular gig but it shows get() returned more than one Freelancers — it returned 3!. I have tried getting the logged in freelancer to is trying to create the git like this freelancer = …

Total answers: 1

Does "select_for_update()" work with the update method in Django?

Does "select_for_update()" work with the update method in Django? Question: The documentation for Django 2.2, which I’m using, gives the following example usage for select_for_update: from django.db import transaction entries = Entry.objects.select_for_update().filter(author=request.user) with transaction.atomic(): for entry in entries: … Using this approach, one would presumably mutate the model instances assigned to entry and call save …

Total answers: 2

In how many ways, can I get ManyToMany field data using Django ORM?

In how many ways, can I get ManyToMany field data using Django ORM? Question: Let’s assume I have a model named A and B. In model B I have ManyToMany field to model A so in how many ways I can get data from model A using model B: class A(models.Model): name= models.CharField(…) class B(models.Model): …

Total answers: 2

How to compare DateTimeField with Date in Django filter funtion?

How to compare DateTimeField with Date in Django filter funtion? Question: In my django app I have a MessageModel with a DateTimeField "send_date". I want to filter the messages to get all messages send a certain day (Date). How do I compare the DateTimeField with a Date to get all messages send within that day. …

Total answers: 3

Django add fields to createsuperuser command

Django add fields to createsuperuser command Question: I have several database tables that I use in Django. Now I want to design everything around the database not in the Django ORM but in the rational style of my MySQL database. This includes multiple tables for different information. I have made a drawing of what I …

Total answers: 2

Django group by Choice Field and COUNT Zeros

Django group by Choice Field and COUNT Zeros Question: Consider the following django model: class Image(models.Model): image_filename = models.CharField(max_length=50) class Rating(models.Model): DIMENSIONS = [ (‘happy’, ‘happiness’), (‘competence’, ‘competence’), (‘warm_sincere’, ‘warm/sincere’), ] rating_value = models.IntegerField(), rating_dimension = models.CharField(max_length=50, choices=DIMENSIONS), image = models.ForeignKey(Image, on_delete=models.CASCADE) Now, I’d like to group all Ratings by the number of ratings per …

Total answers: 1

Django queryset return DurationField value in seconds

Django queryset return DurationField value in seconds Question: I have two models: Post, Comment (Comment has FK relation to Post). Now I want to return all posts with theirs "response time". I get this response time in timedelta format. Can I receive it in seconds instead? I tried ExtractSecond but it is not what I’m …

Total answers: 3

Incorrect results with `annotate` + `values` + `union` in Django

Incorrect results with `annotate` + `values` + `union` in Django Question: Jump to edit to see more real-life code example, that doesn’t work after changing the query order Here are my models: class ModelA(models.Model): field_1a = models.CharField(max_length=32) field_2a = models.CharField(max_length=32) class ModelB(models.Model): field_1b = models.CharField(max_length=32) field_2b = models.CharField(max_length=32) Now, create 2 instances each: ModelA.objects.create(field_1a=”1a1″, field_2a=”1a2″) …

Total answers: 3

Django ORM "filter" method produces SQL query without quotes

Django ORM "filter" method produces SQL query without quotes Question: I am building an app with Django relying on several PosgreSQL databases which I do not manage, let’s call them database A and database B. For each database, I’ve used python manage.py inspectdb to build my models.py files. I am trying to use Django ORM …

Total answers: 1