django-queries

Django: testing get query

Okay, so I am sick of writing this... res = Something.objects.filter(asdf=something) if res: single = res[0] else: single = None if single: # do some stuff I would much rather be able to do something like this: single = Something.objects.filter(asdf=something) if single: #do some stuff I want to be able to grab a single o...

Django Foreign key queries

In the following model: class header(models.Model): title = models.CharField(max_length = 255) created_by = models.CharField(max_length = 255) def __unicode__(self): return self.id() class criteria(models.Model): details = models.CharField(max_length = 255) headerid = models.ForeignKey(header) def __unic...

django views question

In my django views i have the following def create(request): query=header.objects.filter(id=a)[0] a=query.criteria_set.all() logging.debug(a.details) I get an error saying 'QuerySet' object has no attribute 'details' in the debug statement .What is this error and what should be the correct statemnt to query this.And the mode...

Django queryset to find a char value with filtered out spaces?

There are database string values that are sometimes stored with unnecessary spaces: "SDF@#$#@ 132423" Given the value without spaces in the UI of a program: "SDF@#$#@132423" How could I do a Django queryset filter to find the Database value (with spaces) from the UI input value sans spaces? ...

Copying contents of a model

If there exists an old data of a model say , query=Emp.objects.filter(pk=profile.id) Is there a easier way to copy the same values into the same model again.. Now that the id will be different so.. I have this requirement. Thanks.. ...

Django query case-insensitive list match

Hi, I have a list of names that I want to match case insensitive, is there a way to do it without using a loop like below? a = ['name1', 'name2', 'name3'] result = any([Name.objects.filter(name__iexact=name) for name in a]) ...

how to use SQL wildcard % with Queryset extra>select?

I'm using the extra() modifier in a view. (See http://docs.djangoproject.com/en/1.1/ref/models/querysets/#extra-select-none-where-none-params-none-tables-none-order-by-none-select-params-none ) Here's the the code: def viewname(request) ... exact_matchstrings=[] exact_matchstrings.append("(accountprofile.first_name LIKE '"...

Select those objects whose related objects IDs are *all* in given string

Hi Django people, I want to build a frontend to a recipe database which enables the user to search for a list of recipes which are cookable with the ingredients the user supplies. I have the following models class Ingredient(models.Model): name = models.CharField(max_length=100, unique=True) slug = models.SlugField(max_length...

get foreign key objects in a single query - Django

Hi I have 2 models in my django code: class ModelA(models.Model): name = models.CharField(max_length=255) description = models.CharField(max_length=255) created_by = models.ForeignKey(User) class ModelB(models.Model): category = models.CharField(max_length=255) modela_link = models.ForeignKey(ModelA, 'modelb_link')...

Django get() query not working

this_category = Category.objects.get(name=cat_name) gives error: get() takes exactly 2 non-keyword arguments (1 given) I am using the appengine helper, so maybe that is causing problems. Category is my model. Category.objects.all() works fine. Filter is also similarily not working. Thanks, ...

django filter icontains match whole words only

Hi, I am using the filter icontains to search for words but I only want it to match whole words. e.g. if I searched for liver I wouldn't want it returning delivery. my query looks like this MyModel.objects.filter(title__icontains=search_word) I have seen the filter __search but this does not bring back results with 3 characters or l...

Django select_related() returns records even if they are not in joined table

Hi, Is it possible to use Django select_related() function to return records even if they do not appear in both tables? e.g. table 1 =========== id | title 1 title 1 2 title 2 3 title 3 table 2 ============ id | table_1_id | cat 1 1 cat 1 2 1 cat 2 3 3 cat 3 assume table 1 maps to Dj...

A left outer reverse select_related in Django?

Imagine the following model: class Parent(Model): ... class Child(Model) father = ForeignKey(Parent) ... Some parents have children, others do not (they're not parents in the real meaning, it's just a fictional name). I would like to make the following query: I want to list all the Parents, and if they have children, bri...

specify group by field in django 1.2

Hi, I want to use annotate to count the number of occurances in my model, however it is not using the right field in the group by statment. instead of using the field i want (i.e. the one specified in the count function) it uses the primary key of the model. e.g. ObjectHistory.objects.annotate(revisions=Count('resource')) produces s...

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]: ...

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 ...

Strange issue when trying to set a BooleanField value in a django model

I'm trying to change the value of a BooleanField in one of my models, but Django won't let me. Here's the relevant code: query = MyModel.objects.filter(name='example').filter(boolField=False) print query[0].boolField query[0].boolField = True query[0].save() print query[0].boolField This surprisingly prints: False False Any idea wh...

Django query select distinct by field pairs

I have the field 'submission' which has a user and a problem. How can I get an SQL search result which will give a list of only one result per user-problem pair? Models are like this: class Problem(models.Model): title = models.CharField('Title', max_length = 100) question = models.TextField('Question') class Submission(models...

django Queryset with year(date) = '2010'

Hi there, I'm trying to build this query select * from m_orders where year(order_date) = '2010' the field order_date is a DateTime field. I just don't want to use raw sql queries here. Is it even possible to use e.g. MySQL functions in django quersets? ...

Query based on a composition of columns in Django

I'd like to figure out how to query based on a composition of columns, eg the fullname of a user, without any custom SQL. Is this possible? Imagine something like User.objects.filter(firstname_concat_lastname__startswith="Barack Ob") I know that in this particular example, it'd be easy enough to split "Barack Ob" by whitspace and ...