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
118
votes
4 answers

Django TypeError: 'RelatedManager' object is not iterable

I have these Django models: class Group(models.Model): name = models.CharField(max_length=100) parent_group = models.ManyToManyField("self", blank=True) def __unicode__(self): return self.name class Block(models.Model): …
megido
  • 4,135
  • 6
  • 29
  • 33
117
votes
13 answers

Get last record in a queryset

How can I retrieve the last record in a certain queryset?
Stephen
  • 5,959
  • 10
  • 33
  • 43
115
votes
6 answers

django filter on the basis of text length

I would like to filter my model on the basis of the length of the text Something like MyModel.objects.filter(len(text) > 10) where text is a Char or Text field in MyModel model
ashish
  • 2,090
  • 3
  • 17
  • 16
114
votes
4 answers

Getting a count of objects in a queryset in Django

How can I add a field for the count of objects in a database. I have the following models: class Item(models.Model): name = models.CharField() class Contest(models.Model); name = models.CharField() class Votes(models.Model): user =…
thesteve
  • 2,413
  • 6
  • 26
  • 28
114
votes
8 answers

How to output Django queryset as JSON?

I want to serialize my queryset, and I want it in a format as this view outputs: class JSONListView(ListView): queryset = Users.objects.all() def get(self, request, *args, **kwargs): return HttpResponse(json.dumps({'data':…
user2232982
  • 1,325
  • 2
  • 12
  • 16
110
votes
1 answer

Django SUM Query?

I have a query akin to the following: SELECT SUM(name) FROM table WHERE name IS NULL How does that SUM translate into a QuerySet in Django? i.e. What operation xyz does it translate to, in something like MyModel.objects.xyz()?
user541686
  • 205,094
  • 128
  • 528
  • 886
107
votes
2 answers

How to create a Django queryset filter comparing two date fields in the same model

Trying to get a query where the Activity record is stale in my Solr Index. I want to check to see if the Activity.updated date in the database is greater than the Activity.added_toSolr_date for the same record. stale_activities_queryset =…
Carlos Ferreira
  • 1,980
  • 2
  • 14
  • 18
102
votes
2 answers

Why does django's prefetch_related() only work with all() and not filter()?

suppose I have this model: class PhotoAlbum(models.Model): title = models.CharField(max_length=128) author = models.CharField(max_length=128) class Photo(models.Model): album = models.ForeignKey('PhotoAlbum') format =…
Timmmm
  • 88,195
  • 71
  • 364
  • 509
94
votes
11 answers

How to get primary keys of objects created using django bulk_create

Is there a way to get the primary keys of the items you have created using the bulk_create feature in django 1.4+?
mikec
  • 3,543
  • 7
  • 30
  • 34
92
votes
2 answers

How to annotate Count with a condition in a Django queryset

Using Django ORM, can one do something like queryset.objects.annotate(Count('queryset_objects', gte=VALUE)). Catch my drift? Here's a quick example to use for illustrating a possible answer: In a Django website, content creators submit articles,…
Hassan Baig
  • 15,055
  • 27
  • 102
  • 205
90
votes
5 answers

Django Queryset with filtering on reverse foreign key

I have the following Django model: class Make: name = models.CharField(max_length=200) class MakeContent: make = models.ForeignKey(Make) published = models.BooleanField() I'd like to know if it's possible (without writing SQL directly)…
Julian A.
  • 10,928
  • 16
  • 67
  • 107
89
votes
5 answers

Django filter the model on ManyToMany count?

Suppose I have something like this in my models.py: class Hipster(models.Model): name = CharField(max_length=50) class Party(models.Model): organiser = models.ForeignKey() participants = models.ManyToManyField(Profile,…
Ska
  • 6,658
  • 14
  • 53
  • 74
87
votes
4 answers

'RelatedManager' object is not iterable Django

Hey i have looked around through some simliar posts here on SO but havent found anything that has solved my problem. I have the following models, from django.db import models class Areas(models.Model): name = models.CharField(max_length =…
Mike Waites
  • 1,688
  • 3
  • 19
  • 26
87
votes
2 answers

Django: change the value of a field for all objects in a queryset

I have a model MyModel with a boolean field active Elsewhere, I am retrieving a queryset: qs = MyModel.Objects.filter(....) how can I set active=False for all objects in this qs?
43Tesseracts
  • 4,617
  • 8
  • 48
  • 94
87
votes
4 answers

Django query datetime for objects older than 5 hours

I'm trying to write a Django query for widgets that are more than 5 hours old and I'm a bit lost. The widget model has a DateTimeField that is populated with the creation time of the widget.
user523513
  • 957
  • 1
  • 7
  • 8