Questions tagged [django-queryset]

Django querysets are the primary abstraction for retrieving objects from Django's ORM system

Django querysets are the primary abstraction for retrieving objects from Django's ORM system using custom SQL queries. They abstract both the creation of queries as well as the assembly of the model objects that are returned.

See also

6784 questions
2
votes
1 answer

Django delete row from table of third level foreign key

I want to delete a row from model or table where I have the foreign_key value of the row to be deleted and the value of first level table row. For example, I have models say A, B, C where B is referring to A, C is referring to B. class…
aakash singh
  • 267
  • 5
  • 19
2
votes
1 answer

Django update the row if its existing

I have a model say, class ABC(models.Model): x = models.CharFeild(max_length=100) y = model.IntegerFeild(default=1) and another model is, class XYZ(models.Model): a = models.CharFeild(max_length=100) abc =…
aakash singh
  • 267
  • 5
  • 19
2
votes
3 answers

Django: make ModelChoiceField evaluate queryset at run-time

I've overridden the default manager of my models in order to show only allowed items, according to the logged user (a sort of object-specific permission): class User_manager(models.Manager): def get_query_set(self): """ Filter results…
Don
  • 16,928
  • 12
  • 63
  • 101
2
votes
2 answers

Generate a django queryset based on dict keys

I have a dict like: { 'key1' : val1, 'key2' : val2 } And I need a queryset like Q(key1__icontains = val1) | Q(key2__icontains = val2) Thanks
vmassuchetto
  • 1,529
  • 1
  • 20
  • 44
2
votes
4 answers

Aggregation functions returns AttributeError: 'Sum' object has no attribute 'lookup'

I'm trying out the aggregation functions, and I get this strange results (latest official Django 1.2 release). Here's the model: class Reputation(models.Model): user = models.ForeignKey(User) modifier = models.IntegerField() activity =…
Mathieu Dhondt
  • 8,405
  • 5
  • 37
  • 58
2
votes
0 answers

django Q as chained .filter()

There are 2 models connected with a many-to-many relationship. class Record(): working_on_record = models.ManyToManyField( UserProfile, related_name="working_on_record", blank=True) class UserProfile(): name = CharField... The…
Jehob
  • 101
  • 7
2
votes
1 answer

Python3/Django - QuerySet not updating

I'm creating a website that allows for the creation of events, and those events will be shown on both the home page, and on the events list, so that people can click on the events on view the descriptions of them, as well as check in at the event.…
Sam
  • 47
  • 5
2
votes
1 answer

django queryset aggregation count counting wrong thing

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…
Oha Noch
  • 374
  • 1
  • 7
  • 22
2
votes
1 answer

Django queryset get distinct column values with respect to other column

I am using django orm and I am trying to get all the values of a column, but only if a different column is unique with respect to it. Its hard to explain, so here is an example: q | a | 1 w | s | 2 e | a | 3 q | a | 4 w | s | 5 e | a | 6…
Oha Noch
  • 374
  • 1
  • 7
  • 22
2
votes
1 answer

Filter by child class type

Lets say we have a class Place with a class Restaurant inheriting from it : from django.db import models class Place(models.Model): name = models.CharField(max_length=50) address = models.CharField(max_length=80) class Restaurant(Place): …
Hugo Trentesaux
  • 1,584
  • 1
  • 16
  • 30
2
votes
2 answers

Group the django object according to the foreign key

I want to group the model object according to the foreign key with a query or without any iteration. example: Let, I have a model student: class Student(models.Model): id=models.AutoField(primary_key=True) parentId=models.ForeignKey(parent) …
GAJESH PANIGRAHI
  • 1,204
  • 10
  • 17
2
votes
1 answer

Difference with previous object in django queryset annotation

Let's assume that I have following model: class TestModel(models.Model): some_integer = models.IntegerField() and I have 3 instances of this…
mrbox
  • 814
  • 1
  • 6
  • 18
2
votes
1 answer

Django filter by multiple filters , checking for certain conditions

I need to check for a number of conditions and do a filter OR search if the conditions are specified. I am doing it as below: def or_q_if_truthfull(**kwargs): filtered = [Q(**{k: v}) for k, v in kwargs.items() if v] if filtered: …
Joel G Mathew
  • 7,561
  • 15
  • 54
  • 86
2
votes
1 answer

How do I add an annotation with Django annotate on a QuerySet?

I have a model class with a status field, which might have several alternatives, say: class MyModel(models.Model): status_choices = ( ('status1', 'status1'), ('status2', 'status2'), ('status3', 'status3'), …
2
votes
1 answer

Reverse Foreign Key Lookup

#models.py class Orders(models.Model): orderid = models.IntegerField(db_column='orderID', primary_key=True) createdate = models.DateField(db_column='createDate', blank=True, null=True) pickupdate =…
user43944
  • 159
  • 2
  • 13