lsbardel / python-stdnet

Object-data mapper and advanced query manager for non relational databases
http://lsbardel.github.com/python-stdnet/
BSD 3-Clause "New" or "Revised" License
120 stars 20 forks source link

how to query and search ManyToManyField ? #58

Closed bsbrother closed 11 years ago

bsbrother commented 11 years ago

I love this soft increase with days, thanks to the author.

session = odm.Session('redis://127.0.0.1:6379?db=0')

class Group(odm.StdModel): name = odm.SymbolField(unique=True)

class User(odm.StdModel): name = odm.SymbolField(unique=True) groups = odm.ManyToManyField(Group, related_name='users')

g1 = session.add(Group(name='g1')) u1 = session.add(User(name='foo')) g1.users.add(u1) session.query(User).filter(groups__name='g1')[:1] ....

stdnet.exceptions.QuerySetError: Could not filter on model "main.user". Field "groups" does not exist.

if groups is ForeignKey, then not error. i notice the ForeignKey is one-to-many objects relationship, can use u1.groups=xx, but ManyToManyfield only can use u1.groups.add(g1).

  1. how can i filter as sql statement, example: g1 in groups; somefield like '%xx%'?
  2. when Search for text in models , can i use 'in' or 'like'?

thank you again!

lsbardel commented 11 years ago

ManyToManyField is not a standard field and therefore you cannot directly filter on it. If you need all users for a group you do:

g1 = Group.objects.get(name='g1')
users = g1.users.query()

The ManyToManyField is not well documented unfortunately, it will be so in the next release. You should use the stdnet mailing list for general queries and not report an issue. Issues are for bugs, feature requests, improvements and so forth. In that way, other users may see questions/answers and learn form them. Thanks

https://groups.google.com/forum/?fromgroups=#!forum/python-stdnet

Closing this

bsbrother commented 11 years ago

thank you for comment. i'am a chinese user, then can't open groups.google.com site for the fuck GFW. I hope that you can understand and support