I am seeing some inconsistent behavior with a filter. It seems like
when I filter a manytomany field out of a slice, I don't get correct
filter results. Perhaps I am misunderstanding how to use filters with
manytomany fields. I have a small program that shows an example.
Here are the relevant parts of my model.
class PdTask(models.Model):
tiles = models.ManyToManyField('chipmanager.Tile')
class Tile(models.Model):
name=models.CharField(max_length=NAME_LENGTH)
# There are 8 tiles in the db
(Pdb) Tile.objects.all()
[<Tile: ll_1>, <Tile: ll_2>, <Tile: ll_3>, <Tile: ll_4>, <Tile: lr_1>,
<Tile: lr_2>, <Tile: lr_3>, <Tile: lr_4>]
# Create a pdtask named 'bar'
(Pdb) pdtask = PdTask.objects.create(name="bar")
# Add tile ll_1 to its tiles field
(Pdb) pdtask.tiles.add(Tile.objects.get(name="ll_1"))
# Confirm ll_1 was added
(Pdb) pdtask.tiles.all()
[<Tile: ll_1>]
# Find all pdtasks that have an entry in tiles that is in
Tile.objects.all() - should return bar and it does
(Pdb) PdTask.objects.filter(tiles__in=Tile.objects.all())
[<PdTask: bar>]
# PROBLEM IS HERE
# Find all pdtasks that have an entry in tiles that is in slice 0:4 of
Tile.objects.all()
# I think this should return bar as well, but it returns an empty
list. WHY???
(Pdb) PdTask.objects.filter(tiles__in=Tile.objects.all()[0:4])
[]
# Find all pdtasks that have an entry in tiles that is in the filtered
list of Tile objects that start with 'll'.
# I think this should return bar - and it does
(Pdb) PdTask.objects.filter(tiles__in=Tile.objects.filter
(name__startswith="ll"))
[<PdTask: bar>]
# Find all pdtasks that have an entry in tiles that is in slice 0:4 of
the the filtered list of Tile objects that
# start with 'll'. I think this should return bar - and it does
(Pdb) PdTask.objects.filter(tiles__in=Tile.objects.filter
(name__startswith="ll")[0:4])
[<PdTask: bar>]
(Pdb)
Am I using the __in filter correctly for a manytomany field?
Margie
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---