QuerySet returning no instances of a child model
How to force QuerySet not to return instances of child models? ...
How to force QuerySet not to return instances of child models? ...
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? ...
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? ...
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? ...
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...
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...
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...
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...
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...
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 ...
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...
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]: ...
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 ...
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 ...
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...
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...
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, 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...
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...
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...