I have 4 models:
class App(models.Model):
...
class AppVersion(models.Model):
app = models.ForeignKey(App)
version_code = models.IntegerField()
class Meta:
ordering = ('-version_code',)
...
class Apk(models.Model):
…
I'm using django 2.1, python 3.6 and SQL Server 2012 as backend. I have following models:
class ModelA(models.Model):
name = models.CharField(...)
value = models.PositiveIntegerField(...)
class ModelB(models.Model):
name =…
Getting into deep water with Subquery. I have a set of Carparks. Carparks have multiple Bookings. Bookings have many BarrierActivity records, which are the various coming and going events at the barriers. These are all simple FKs up the stack.
It is…
When I'm writing Django Subquery expressions, sometimes I have no idea how Django is going to group the result.
Example:
subquery = (
RelatedModel.objects.filter(
category="some_category",
…
In the queryset, I want to get the average of what subquery returns and then group by 'store_id' and 'avg_sales'. However, when I used the following queries:
subquery = StoreStatistics.objects.filter(
store=OuterRef("store_id"),
…
Models.py
class ChatRoomId(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
created_date = models.DateTimeField(auto_now_add=True)
class MessageContent(models.Model):
content =…
I'm trying to create a high score statistic table/list for a quiz, where the table/list is supposed to be showing the percentage of (or total) correct guesses on a person which was to be guessed on. To elaborate further, these are the models which…
I need to fetch the top performer for each month, here is the below MySql query which gives me the correct output.
select id,Name,totalPoints, createdDateTime
from userdetail
where app=4 and totalPoints in ( select
max(totalPoints)
FROM…
Assume I have a Transaction model which has following fields [token, pair_token, amount, related_transaction], I want to generate a query like this in mysql:
SELECT token_id, pair_token_id, (
SELECT ABS(t3.amount / t2.amount) price
FROM
…
I have a single model with a jsonb field. There is a value inside this jsonb field that can be shared amongst other rows. I am trying to get the count of a subquery while filtering by this jsonb field.
Some pseudo code of what I have been attempting…
In our system, there are "Car" objects that are associated with "User" objects. With a subquery, I want to count a specific set of Car objects, perform an arithmetic operation on the result and update the User object with that value. The problem is…
I'm trying to implement a subquery with Django ORM, but I can't find a working solution.
The SQL query that I would need to reverse-engineer is:
select t1.location, sum(t1.value_relative::numeric) as total
from (
select
…
EDIT:
As per schillingt's answer below I have switched to using Case/When:
context['db_orders'] = Order.objects.filter(
retailer_code=self.object.retailer_code).annotate(
…
I'm trying to filter a model which has a DateField (date) to retrieve a queryset of instances whose date is in any one of a list of DateRanges but I'm struggling to figure out the exact logic I need.
So for example, if I have the following…
I have the following subquery:
Subquery(
ContestTaskRelationship.objects.filter(
contest=contest,
solved=OuterRef('id')
).values('cost').all()
)
I need then to annotate my QuerySet with sum of cost values…