|
List Info
Thread: Raw SQL to Django API Question and a ManyToMany Model Question
|
|
| Raw SQL to Django API Question and a
ManyToMany Model Question |

|
2006-02-14 23:34:05 |
Working on a last-minute mapping app for PyCon here, I'm
currently
using a raw SQL query to SELECT information from People
(Model) who all
share the same Tag (Model in a ManyToMany with People).
I'm feeding
the query a range of lat/lon and the tag I want,
c = db.cursor()
c.execute("""select p.id, p.name,
p.lat, p.lon from maps_people as
p
left join maps_tags_person as pt on pt.person_id =
p.id
left join maps_tags as t on t.id = pt.tag_id
where t.tag = '%s'
and p.lat >= %s
and p.lon >= %s
and p.lat <= %s
and p.lon <= %s
order by p.id""" % (request_tag,
minlat, minlon, maxlat,
maxlon))
r = c.fetchall()
c.close()
I'd just like to figure out the Django API equivalent (if
there is one)
to do something like this.
I was thinking, like, people.get_list(lat__gte=minlat,
lon__gte=minlon,
lat__lte=maxlat, lon__lte=maxlon, has__tag=tag) or
whatever... ideas?
---
Second question,
What is a good method of keeping a count of ManyToMany
relationships
that each entry in the table has? In the example above, I
added a
meta.IntegerField to the Tag Model, and I want it to hold
the # of
people that have that tag. I don't want to calculate this
as-needed
(way to often, if I use it)... I can't really import the
model IN the
model, can I? (I tried, and then my brain exploded)
My original thought was to just add a _pre_save that updated
with the
count everytime a Tag was saved (Tags aren't really edited,
so pretty
much ALL saves affect the number of relationships) ... but I
wasn't
sure what a "best practice" method of this was.
---
Thanks,
Brett
|
|
| Raw SQL to Django API Question and a
ManyToMany Model Question |

|
2006-02-15 22:24:32 |
Not sure if anyone will see this now that it's off the
front page, but
heres what I've gathered so far:
I don't think the Django DB API can handle many2many stuff
(at least
right now)
As for question 2, apparently you _can_ import a model in
itself, so
problem solved.
Thanks to bitprophet on IRC.
Brett
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-users googlegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribe googlegroups.com
For more options, visit this group at http://gr
oups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---
|
|
| Raw SQL to Django API Question and a
ManyToMany Model Question |

|
2006-02-16 01:32:51 |
|
On 2/16/06, Brett Hoerner <gmail.com">bretthoerner gmail.com> wrote:
Not sure if anyone will see this now that it's off the front page, but heres what I've gathered so far: Sorry - I meant to answer this one yesterday, but I got distracted.
I don't think the Django DB API can handle many2many stuff (at least right now)
You can query across many-to-many relations, in the same way that you query across many-to-one relations. You should be able to query something like:
people.get_list(lat__gte=minlat, lon__gte=minlon, lat__lte=maxlat, lon__lte=maxlon, tags__value__exact=tag)
The exact syntax will vary a bit depending on your model names, etc - but the short version is that you should be able to use __ notation to specify the attribute referencing the model you are joined to (tags=ManyToManyField(Tags)), then the column name you want to check (
Tags.value), then the match condition (exact=tag).
This works a bit better in the magic-removal stream - in 0.91/trunk, you can only query in the direction that the relation is defined (i.e., if People has a ManyToManyField("Tag"), you can query
people.get_list(tags__...), but not tags.get_list(people__...). In the magic-removal stream, queries are bidirectional.
Also - watch out for duplicates . The people.get_list call will return a list with an element for every tag match it finds - if there is more than one tag for any given person that matches, that person will appear twice in the list. You may need to add "distinct=True" as a parameter to your get_list call if you want to avoid this.
Hope this helps, Russ Magee %-)
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users googlegroups.com To unsubscribe from this group, send email to django-users-unsubscribe googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users -~----------~----~----~----~------~----~------~--~---
|
| Raw SQL to Django API Question and a
ManyToMany Model Question |

|
2006-02-16 03:44:19 |
I've tried:
people.get_list(lat__gte=minlat, lon__gte=minlon,
lat__lte=maxlat,
lon__lte=maxlon, tags__value__exact='django')
TypeError: got unexpected keyword argument
'tags__value__exact'
And:
people.get_list(lat__gte=minlat, lon__gte=minlon,
lat__lte=maxlat,
lon__lte=maxlon, tag__value__exact='django')
TypeError: got unexpected keyword argument
'tag__value__exact'
---
class Tag(meta.Model):
person = meta.ManyToManyField(Person, blank=True,
null=True)
tag = meta.CharField(maxlength=50, db_index=True)
count = meta.IntegerField(blank=True, null=True)
def _pre_save(self):
self.tag = self.tag.lower()
def __repr__(self):
return self.tag
class META:
module_name = "tags"
verbose_name_plural = "Tags"
verbose_name = "Tag"
admin = meta.Admin()
---
Any ideas? Thanks much for the reply, though.
Brett
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-users googlegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribe googlegroups.com
For more options, visit this group at http://gr
oups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---
|
|
| Raw SQL to Django API Question and a
ManyToMany Model Question |

|
2006-02-16 04:03:27 |
|
On 2/16/06, Brett Hoerner <gmail.com">bretthoerner gmail.com> wrote:
---
class Tag(meta.Model): person = meta.ManyToManyField(Person, blank=True, null=True) This is what I meant about the bidirectional query problem - in 0.91/trunk ManyToMany fields can only be traversed in the direction they are defined - in this case, from Tag to Person, but not from Person to Tag. Using this model definition, you should be able to query:
tags.get_list(person__name__exact="xyz")
will work, but
people.get_list(tags__tag__exact="xyz")
will generate an error (as you have experienced). This deficiency has been fixed in the magic-removal development branch.
Try putting the ManyToMany field definition in the Person model, rather than the Tag model.
Yours, Russ Magee %-)
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users googlegroups.com To unsubscribe from this group, send email to django-users-unsubscribe googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users -~----------~----~----~----~------~----~------~--~---
|
[1-5]
|
|