django-queryset

Annotate a django query via a reverse relationship

I have two models Property and Image, related by foreign key such that each Property has multiple instances of Image. I'm trying to retrieve a queryset of all the properties - listing all fields - and the number of images that each property has. I am aware that I could do this as two distinct queries, but I don't feel that this is a part...

How to get first top five objects with django.views.generic.date_based.archive_index?

Hello, I'm trying to display the latest 5 posts using a generic view like this: urlpatterns = patterns('', (r'^$', 'django.views.generic.date_based.archive_index', { 'queryset': Post.objects.all()[:5], 'date_field': 'created_on', 'template_name': 'index.html'} }) However I am getting AssertionError at...

object_detail() got multiple values for keyword argument 'queryset' while inputting only one

from django.conf.urls.defaults import * from django.conf import settings from Website.Blog.models import Post # Uncomment the next two lines to enable the admin: from django.contrib import admin admin.autodiscover() index = { 'queryset': Post.objects.all(), 'date_field': 'created_on', 'template_name':...

manually create a Django QuerySet or rather manually add objects to a QuerySet

Basically I need a graceful way to do the following:- obj1 = Model1.objects.select_related('model2').get(attribute1=value1) obj2 = Model1.objects.select_related('model2').get(attribute2=value2) model2_qs = QuerySet(model=Model2, qs_items=[obj1.model2,obj2.model2]) I may not be thinking right, but doing something like the following se...

Django Queryset Filter (Maybe Needs a Subquery)

I want to generate a queryset to find mismatches. As an example class Vehicle(models.Model): car = models.CharField(max_length=100) model= models.CharField(max_length=100) passengers = models.IntegerField() i want to generate a query where i can find cars erroneously listed with two different models. something along th...

Custom Form for Group Permission with filtered Queryset

I need to offer a from in which a user can manage the permission associated to some Group. I'd like to use the forms.ModelForm feature which comes from django, but I cannot understand how to modify the queryset over which the field cycles. I've also taken a deep look in contrib.admin and contrib.auth to discover where those forms are ge...

Django: Get all blogs and their latest blog entries

Suppose I have these two models: class Blog(models.Model): name = models.CharField(max_length=64) # ... class Entry(models.Model): blog = models.ForeignKey(Blog) added = models.DateTimeField(auto_now_add=True) # ... Now I want to get a list of all blogs along with the latest blog entry for each respective blog. Wh...

Is it possible to combine annotations with defer/only in django 1.2.1?

I have two simple models: Book, and Author Each Book has one Author, linked through a foreignkey. Things work normally until I try to use defer/only on an annotation: authors=Author.objects.all().annotate(bookcount=Count('books')) that works. The query looks like: select table_author.name, table_author.birthday, COUNT(table_book.i...

Django last thirty days added with number

With a model like this: class User(models.Model): entered = models.DateTimeField(auto_now_add=True) How to grab stats data so it show recent added users for last 30 days with number of users added on that day. Example: 2010-08-02: 220 2010-08-08: 1220 2010-08-14: 20 ...

Django queries: how to annotate with a filtered count?

Suppose I have a Book model with a language field and a foreign key to a Publisher model. Currently I use a Count annotation in a custom Publisher manager to allow me to add to the admin a sortable column with the number of books by each publisher. (see http://stackoverflow.com/questions/3491766/how-to-add-a-sortable-count-column-to-the...

How can I sort my Django model using complex data from a second model?

I have 3 django models (simplified for this example): class Fighter (models.Model): name = models.CharField(max_length=100) weight_class = models.ForeignKey(WeightClass, related_name="fighter_weight_class") class Bout (models.Model): fighter_1 = models.ForeignKey(Fighter, related_name="bout_fighter_1") fighter_2 = model...

Manager gives a queryset from files and not from a database

I would like to override the manager class in order to allow content data to be loaded from text files (here on of "/one directory/myPrefix_*") into content field instead of from a database table. class Things(model.Models): file = CharField(max_length = 25, primary key = True) content = TextField() objects = myManager("/...

Django - Get items in many sets

My models: class ItemSet(models.Model): name = models.CharField(max_length=30) item = models.ManyToManyField(Item) order = models.IntegerField(default=0) class Item(models.Model): name = models.CharField(max_length=30) desc = models.CharField(max_length=100) A set includes many items and a item can be in many sets...

What is the internal function in django to add new tables to a queryset in a sensible way?

In django 1.2: I have a queryset with an extra parameter which refers to a table which is not currently included in the query django generates for this queryset. If I add an order_by to the queryset which refers to the other table, django adds joins to the other table in the proper way and the extra works. But without the order_by, th...

Django - dreaded 'iteration over non-sequence'

Hi I'm looking to populate a list of members, based on where their club comes from. This is my code: members = [] if userprofile.countries.count() > 0: for c in userprofile.countries.all(): clubs = Club.objects.filter(location__country = c) for club in clubs: members_list = Member.objects.get_membe...

django most efficient way to count same field values in a query

Lets say if I have a model that has lots of fields, but I only care about a charfield. Lets say that charfield can be anything so I don't know the possible values, but I know that the values frequently overlap. So I could have 20 objects with "abc" and 10 objects with "xyz" or I could have 50 objects with "def" and 80 with "stu" and i ha...

Django: how to aggregate / annotate over a many-to-many relationship?

I have a Person model and a Tag model, with a m2m between them. I need to extract the tag which is connected to the most records within a given Person queryset, together with the count. Is there an elegant, efficient way to extract this using the Django ORM? Better yet, is there a way to get the entire tag distribution through some a...

django queryset for many-to-many field

I have the following Django 1.2 models: class Category(models.Model): name = models.CharField(max_length=255) class Article(models.Model): title = models.CharField(max_length=10, unique=True) categories = models.ManyToManyField(Category) class Preference(models.Model): title = models.CharField(max_length=10, unique=Tru...

Django queryset update field increasing/descreasing its current value

Hey, I am trying to change order of nodes in my tree. Everything works fine, but I would like to know if there is some beautiful, easy way of updating multiple fields by increasing its actual value by 1. Let me illustrate. Objtree.objects.select_related().filter(pk__in = ids).update(sort_order = 1) This code will change every sort_or...

Django design question

Hi, I have a model in django that have a boolean private/public attribute: class TestModel(models.Model): name = models.CharField() is_public = models.BooleanField(default=False) I want that every-time I query this model in an application it returns only public instances to the common user and all available instances to the su...