Question

In my model I have a column name category and the values are in the form of "x -> y -> z"
x,y,z all are separated by arrow (->) (although whole itself is a string)

what I want is to filter objects on the basis of 'y' string

something like

MyModel.objects.filter(category = lambda key: y in key)

but it gives me error TypeError: <lambda>() takes exactly 1 argument (0 given)

Example: MyModel (3 column, category column is foreign key to another model(which has only one column same name=category))
ID   name   category
1    xyz    world->europe->france
2    abc    animal->fourleg->dog
3    pqr    car->europe->benz

I applied this below query

MyModel.objects.filter(category = lambda key: 'europe' in key)and

And I was expecting two returned objects (Id 1 and 3).

I hope its clear now?

I'm not able to figure it out why this is happening. Or is there any other way to do same thing? I'm new to django so it may be meaningless/irrelevant or silly question but I'm not able to find the solution.

Python 2.x

Django 1.4.x

Was it helpful?

Solution

Depending on how your string is structured you might be able to use a contains lookup. Something like:

# 'name' is whatever your string column on category is called
MyModel.objects.filter(category__name__contains='->{}->'.format(y_string))

See the docs for more details on lookup types.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top