django-queryset

How to aggregate over a single queryset in Django?

Short description: given a queryset myQueryset, how do I select max("myfield") without actually retrieving all rows and doing max in python? The best I can think of is max([r["myfield"] for r in myQueryset.values("myfield")]), which isn't very good if there are millions of rows. Long description: Say I have two models in my Django app,...

Django QuerySet API: How do I join iexact and icontains?

Hello, I have this join: lawyers = Lawyer.objects.filter(last__iexact=last_name).filter(first__icontains=first_name) This is the site If you try Last Name: Abbas and First Name: Amr it tells you that amr abbas has 1 schoolmates. But if you try First name only it says that there are no lawyers in the database called amr (obviously t...

django, a good way of querying a distinct model

Assume I have a such model: class Foo(models.Model): name = models.CharField("ad",max_length=25) type = models.ForeignKey(Type) So at the database I have Foo objects with same name field but different types ie: name type A 1 A 2 B 1 C 2 A 3 B 3 I will use this information inorder to generate a html s...

Retrieving unique results in Django queryset based on column contents

I am not sure if the title makes any sense but here is the question. Context: I want to keep track of which students enter and leave a classroom, so that at any given time I can know who is inside the classroom. I also want to keep track, for example, how many times a student has entered the classroom. This is a hypothetical example tha...

Fastest Way to Update a bunch of records in queryset in Django

I have a queryset with a few million records. I need to update a Boolean Value, fundamentally toggle it, so that in the database table the values are reset. What's the fastest way to do that? I tried traversing the queryset and updating and saving each record, that obviously takes ages? We need to do this very fast, any suggestions? ...

Is it possible to reference a property using Django's QuerySet.values_list?

I have a custom property on my Django model that returns the full name of a Person: class Person(models.Model): first_name = models.CharField(max_length=30) last_name = models.CharField(max_length=30) def _get_full_name(self): return "%s %s" % (self.first_name, self.last_name) full_name = property(_get_full_name) When I create ...

Custom QuerySet and Manager without breaking DRY?

I'm trying to find a way to implement both a custom QuerySet and a custom Manager without breaking DRY. This is what I have so far: class MyInquiryManager(models.Manager): def for_user(self, user): return self.get_query_set().filter( Q(assigned_to_user=user) | Q(assigned_to_group__in=...

Get last record in a queryset

How can I retrieve the last record in a certain queryset? ...

Django, filtering the most recently added element

I want to retrieve the most recently added element and if there is none at all, assign some default values such as: query = X.objects.filter(name="aa",type="b")[0] if query: resultname =query.name resulttype = query.type else: resultname = "a default name" resulttype = "a default type" This is not working as it will ra...

Is there a way to make a query respect the order of the inputted parameters?

(Please let me know if this is completely absurd, that is probably a reason why I haven't found anything on this.) This story has two models Ranking and Artist, Ranking is generically related to Artist (object_id, content_type... the whole shebang). I have a list of objects returned by Ranking.objects.values_list() ordered by a certain...

JSON Serializing Django Models with simplejson

I'd like to use simplejson to serialize a Django model. Django's serializer doesn't support dictionaries... and simplejson doesn't support Django Querysets. This is quite a conundrum. In the model there's sponsors that have a Foreign Key to sponsor level, I'm trying to group all the sponsors that belong to a certain sponsor level togeth...

Django Newbie - With a multiple fields form, how do I eliminate empty fields within the queryset

Hello, I'm a Django newbie, so please forgive me if this is a stupid question. I have a search form that has multiple fields on it. I only wish to filter my queryset by those fields submitted that aren't empty. How do I do that? I'm aware you can chain querysets and Q objects together, but I don't know how to eliminate empty key/value p...

Count number of records by date in Django

I'm using Django 1.1 with MySQL as the database. I have a model similar to the following: class Review(models.Model): venue = models.ForeignKey(Venue, db_index=True) review = models.TextField() datetime_created = models.DateTimeField(default=datetime.now) I'd like to query the database to get the total number of reviews...

Django queryset returning nothing but []

I've been able to do this through the django environment shell, but hasn't worked on my actual site. Here is my model: class ShoeReview(models.Model): def __unicode__(self): return self.title title = models.CharField(max_length=200) slug = models.SlugField(unique=True) keywords = models.Te...

Can Django do nested queries and exclusions

I need some help putting together this query in Django. I've simplified the example here to just cut right to the point. MyModel(models.Model): created = models.DateTimeField() user = models.ForeignKey(User) data = models.BooleanField() The query I'd like to create in English would sound like: Give me every record that w...

Sorting&grouping QuerySet data in Django

I have BlogEntry model, which has two special fields : "publication_date"(timestamp), "sticky"(boolean). i need to get them in order by "publication_date", and group by "sticky" field that have to be first in query set. what i try to get: [ <BlogEntry:"05.03.2010 sticky:1">, <BlogEntry:"04.03.2010 sticky:1">, <BlogEntry:"03.03.2010 st...

Django filter vs exclude

Is there a difference between filter and exclude in django? If I have self.get_query_set().filter(modelField=x) and I want to add another criteria, is there a meaningful difference between to following two lines of code? self.get_query_set().filter(user__isnull=False, modelField=x) self.get_query_set().filter(modelField=x).exclude(u...

Sum computed column in Django QuerySet

Given the following Contribution model: class Contribution(models.Model): start_time = models.DateTimeField() end_time = models.DateTimeField(null=True) is it possible, using the Django database API, to reproduce the following SQL statement? SELECT SUM(end_time - start_time) AS total_duration FROM contribution; I've figured...

How do I select from multiple tables in one query with Django?

I have two tables, one "Company" and one "Employee": class Company(models.Model): name = models.CharField(max_length=60) class Employee(models.Model): name = models.CharField(max_length=60) company = models.ForeignField(Company) And I want to list every Employee in a table, with the Company next to it. Which is simple eno...

How to implement full text search in Django?

I would like to implement a search function in a django blogging application. The status quo is that I have a list of strings supplied by the user and the queryset is narrowed down by each string to include only those objects that match the string. See: if request.method == "POST": form = SearchForm(request.POST) if fo...