django-queryset

Django Queryset across Models?

I have several Models and want to return a queryset of all the Models belonging to a User, I'm wondering if its possible to return one Queryset from multiple Models? ...

How do I query a Django model dynamically?

Is there a way of doing the following? model = "User" model.objects.all() ...as opposed to User.objects.all(). EDIT: I am trying to make this call based on command-line input. Is it possible to avoid the import statement, e.g., model = django.authx.models.User ? Django returns an error, "global name django is not defined." ...

How to combine 2 or more querysets in a Django view?

Hi, I am trying to build the search for a Django site I am building, and in the search I am searching in 3 different models. And to get pagination on the search result list I would like to use a generic object_list view to display the results. But to do that i have to merge 3 querysets into one. How can i do that? Ive tried this: resu...

Filtering a complete date in django?

There are several filter methods for dates (year,month,day). If I want to match a full date, say 2008/10/18, is there a better way than this: Entry.objects.filter(pub_date__year=2008).filter(pub_date__month=10).filter(pub_date__day=18) ...

Django Custom Queryset filters

Is there, in Django, a standard way to write complex, custom filters for QuerySets? Just as I can write MyClass.objects.all().filter(field=val) I'd like to do something like this : MyClass.objects.all().filter(customFilter) I could use a generator expression (x for x in MyClass.objects.all() if customFilter(x)) but that would...

Select Distinct Years and Months for Django Archive Page

I want to make an archive_index page for my django site. However, the date-based generic views really aren't any help. I want the dictionary returned by the view to have all the years and months for which at least one instance of the object type exists. So if my blog started in September 2007, but there were no posts in April 2008, I cou...

How do I do a not equal in Django queryset filtering?

In django model querysets, I see that there is a __gt and __lt for comparitive values, but is there a __ne/!=/<> (not equals?) I want to filter out using a not equals: Example: Model: bool a; int x; I want results = Model.objects.exclude(a=true, x!=5) The "!=" is not correct syntax. I tried __ne, <>. I ended up using: re...

Django: How to merge two related querysets in Django 0.96?

Hi all. I would like to merge one querysets related objects with another querysets related objects. Some sample code to explain: ## Models # sample models to illustrate problem class PetShop(models.Model): id = models.AutoField(primary_key=True) shop_name = models.CharField(maxlength=255) cats = models.ManyToManyField(Cat) ...

Django Filters - or?

How would I do an "or" in a django filter. Basically, I want to be able to list the items that either a user has added (they are listed as the creator) or the item has been approved so I basically need to select item.creator = owner or item.moderated = False How would I do this in django (preferably with a filter/queryset) ...

Django equivalent of COUNT with GROUP BY

I know Django 1.1 has some new aggregation methods. However I couldn't figure out equivalent of the following query: SELECT player_type, COUNT(*) FROM players GROUP BY player_type; Is it possible with Django 1.1's Model Query API or should I just use plain SQL? ...

How can I use Django admin list and filterering in my own views?

I’m just beginning to learn Django and I like the automatic listing in Django admin and the way you can configure filters and what columns to show. Is it possible to use it in my own applications? I’ve looked in the source for the admin and figured out that I probably want to subclass the “ChangeList”-object in some way and use it in my...

Django filter -- How do I go about filtering for emply or NULL names in a queryset

Hi, I have first_name, last_name & alias (optional) which I need to search for. So, I need a query to give me all the names that have an alias set. Only if I could do: Name.objects.filter(alias!="") So, what is the equivalent to the above? Thanks, VN44CA ...

how to use filter in django

hi, class Status(models.Model): someid = models.IntegerField() value = models.IntegerField() status_msg = models.CharField(max_length = 2000) so my database look like: 20 1234567890 'some mdg' 20 4597434534 'some msg2' 20 3453945934 'sdfgsdf' 10 4503485344 'ddfgg' so I have to fetch values contain a givin...

multiple exclude

is there a way to do a query and exclude a list of things instead of calling exclude multiple times? ...

django - reorder queryset after slicing it

I fetch the latest 5 rows from a Foo model which is ordered by a datetime field. qs = Foo.objects.all()[:5] In the following step, I want to reorder the queryset by some other criteria (actually, by the same datetime field in the opposite direction). But reordering after a slice is not permitted. reverse() undoes the first ordering, g...

Specifying Django Related Model Sort Order

I have Django models that are lists of items and I want the items on each list to have a separate sort order. Maybe list 1 will sort line items by name, list 2 by date, and list 3 by priority. The models looks more or less like this: class ListItem(models.Model): priority = models.IntegerField(choices=PRIORITY_CHOICES) name = ...

Efficient week statistics for a QuerySet

I am working on an open source Django time tracking app, Djime, and I'm trying to come up with a more efficient way to produce statistics. So far, we've had some rather long-winded procedural code that would get all TimeSlices for a period, and collate them together in a huge nested list/dictionary mess. What I'd like to do is to set up...

Get the index of an element in a queryset

Hi I have a QuerySet, let's call it qs, which is ordered by some attribute which is irrelevant to this problem. Then I have an object, let's call it obj. Now I'd like to know at what index obj has in qs, as efficiently as possible. I know that I could use .index() from Python or possibly loop through qs comparing each object to obj, bu...

django - convert a list back to a queryset

I have a handful of records that I would like to sort based on a computed value. Got the answer over here... like so: sorted (Profile.objects.all (), key = lambda p: p.reputation) on a Profile class like this: class Profile(models.Model): ... @property def reputation(self): ... Unfortunately the generic view i...

Help with Django QuerySets

I have a Model with a Foreign Key of "Parent" class Item(models.Model): parent = models.ForeignKey(Parent) This is the FK model class Parent(models.Model): name = models.CharField(blank=True, max_length=100) def __unicode__(self): return str(self.name) I am trying to run a query that gets all Items with a parent of "xyz" I get n...