django-queryset

QuerySet returning no instances of a child model

How to force QuerySet not to return instances of child models? ...

Filtering second level of Many-to-Many relations in Django

Consider three django models: AA, BB and CC. AA has an M2M reference to BB and BB has an M2M reference to CC. I have a single instance of AA. How do I execute a filter() QuerySet over the set of CC instances that are M2M related the the BB instances that are M2M related to that single AA instance? ...

Is there a simple way for QuerySet addition and subtraction in django?

Consider two QuerySet objects of the same class. Is there a simple way to unify them into a single QuerySet, similar to addition but without duplicates. Also, is there a simple way to subtract them? Removing all elements that appear in both sets from one of the sets? ...

Filtering rows withing Admin using a Queryset - Django

Hi folks, I'm trying to find a way to filter down rows of objects within Django Admin, using a queryset. e.g. Person.objects.filter(Q(name='John')|Q(surname='Doe')) I'm finding quite complicated to figure out. Any ideas? ...

Django: Filtering datetime field by *only* the year value?

Hi folks, I'm trying to spit out a django page which lists all entries by the year they were created. So, for example: 2010: Note 4 Note 5 Note 6 2009: Note 1 Note 2 Note 3 It's proving more difficult than I would have expected. The model from which the data comes is below: class Note(models.Model): business = models.Forei...

How to find all records that share the same field value as some other record?

I need to extract all records which have a field which does NOT have a unique value. I can't figure out an elegant way to do it - using annotation or some other way. I see a "value_annotate" method to the object manager but it's unclear if it's at all related. Currently I'm using the inelegant way of simple looping through all values a...

Django excluding specific instances from queryset without using field lookup

Hi, I sometimes have the need to make sure some instances are excluded from a queryset. This is the way I do it usually: unwanted_instance = Mymodel.objects.get(pk=bad_luck_number) uninteresting_stuff_happens() my_results = MyModel.objects.exclude(id=unwanted_instance.id) or, if I have more of them: my_results = MyModel.objects.exc...

How to obtain and/or save the queryset criteria to the DB?

I would like to save a queryset criteria to the DB for reuse. So, if I have a queryset like: Client.objects.filter(state='AL') # I'm simplifying the problem for readability. In reality I could have # a very complex queryset, with multiple filters, excludes and even Q() objects. I would like to save to the DB not the results of t...

Django QuerySet filter + order_by + limit

So I have a Django app that processes test results, and I'm trying to find the median score for a certain assessment. I would think that this would work: e = Exam.objects.all() total = e.count() median = int(round(total / 2)) median_exam = Exam.objects.filter(assessment=assessment.id).order_by('score')[median:1] median_score = median_ex...

Custom properties in a query

Given the simplified example below, how would I access my custom "current_status" property within a queryset? Is it even possible? At the moment, I want to list the all the current Events and show the current status. I can get the property to display in a template ok, but I can't order the queryset by it. Alternatively, would I need to ...

Select all photos from each Album (Django)

I need some help (sorry for my poor english). I 'm trying to get all photos from each album. I don't know how to make the query. I need this data (and order the photos by atribute "order" it will fantastic). House1-title photo1: descritpion photo2: descritpion photo3: descritpion House2-title photo1: descritpion photo2: descritpi...

Problem with django aggregation queries

Hello I have a number of elements that can be of one or more types. class Type(models.Model): name = models.CharField(max_length=128, unique=True) class Element(models.Model): name = models.CharField(max_length=128, unique=True) type = models.ManyToManyField('Type') Let's say that I have 3 types and 3 elements: In [3]: ...

Good practices for a flexible search page - Django

Hi folks, I'm just wondering if there is any example I could take from others on the topic. I have a page within Django which uses filters, in order to perform searches. At the moment I'm doing a simple check for the GET parameters and adding a .filter() to a queryset accordingly: if color: query.filter(color=color) This feels ...

Django - How to transform a QuerySet to a Q object?

Is there a way to transform a QuerySet to a Q object in django? My specific motivation: I would like to subtract one QuerySet (qs_A) from another QuerySet (qs_B). The only way I can think of is by using exclude() and a Q object equivalent of qs_A. Example: def my_function(qs_A, qs_B): # Here I need to transform qs_A to a Q object ...

chain filter and exclude on django model with field lookups that span relationships

I have the following models: class Order_type(models.Model): description = models.CharField() class Order(models.Model): type= models.ForeignKey(Order_type) order_date = models.DateField(default=datetime.date.today) status = models.CharField() processed_time= models.TimeField() I want a list of the order types tha...

How to Access all the fields that have been filtered in a Django Custom Manager

Hi, I am writing a custom manager, and implementing the get_query_set method. Basically, I want to see that certain fields are passed into the query, think the Custom Site Manager but not wanting to add filters I want to ensure that some fields ARE filtered on. Below is one way, but I was wondering if there is a better way to get the f...

Django: union of different queryset on the same model

I'm programming a search on a model and I have a problem. My model is almost like: class Serials(models.Model): id = models.AutoField(primary_key=True) code = models.CharField("Code", max_length=50) name = models.CharField("Name", max_length=2000) and I have in the database tuples like these: 1 BOSTON The new Boston ...

in django how do I create a queryset to find double barrel names?

In django, I have a table of people, each of which has a namefirst and namelast. I want to do the sql: select * from names where left(namefirst,1)=left(namelast,1). Right now my best effort is qs=People.objects.extra(select={'db':'select left(namefirst,1)=left(namelast,1)'}) but then if i stick a .filter(db=1) on that it genera...

writing a django query and get reverse related objects in one hit of database!

hi i have written these models in models.py: class User(models.Model): first_name = models.CharField(max_length=80) class Skill(models.Model): user = models.ForeignKey(User) title = models.CharField(max_length=80) level = models.IntegerField(default=3) class Work(models.Model): user = models.Foriegnkey(User) w...

Is there a way to filter a django queryset based on string similarity (a la python difflib)?

I have a need to match cold leads against a database of our clients. The leads come from a third party provider in bulk (thousands of records) and sales is asking us to (in their words) "filter out our clients" so they don't try to sell our service to a established client. Obviously, there are misspellings in the leads. Charles become...