views:

70

answers:

3

I have somehow two variables for example x and y. I have also made a model with 3 fields (longitude,latitude,name) and have it activated in mysql database. I need to send these two variables(x,y) to the django server so as to search if there is an object with longitude=x and latitude=y.If there is one i want to get back it's name.

How can i do this?

A: 
try: 
    x = OurModel.objects.filter(longitude=x, latitude=y)
    for cur in x:
       print cur.name
except OurModel.DoesNotExist:
    print 'sorry, there is no record you are looking for :('

Should work :)

Of course, print will show name in console.

Rin
if there is more than 1 row with x,y in db, Django will raise for you: MultipleObjectsReturned: get() returned more than one OurModel
panchicore
U are right, i though there could be only one... will fix my answer.
Rin
A: 

Basicly you cant ask to db if there is an object with longitude=x and latitude=y like this:

url:

url(r'^get_name_by_xy/(?P<x>\d+)/(?P<y>\d+)/$', 'get_name_by_xy', name='get_name_by_xy'),

view:

def get_name_by_xy(request, x, y):
    import YourModel
    try:
        info = YourModel.objects.filter(longitude=x, latitude=y)
        for i in info:
             print i.name
    except YourModel.DoesNotExist:
         print 'No coincidences'

there you'll get all rows with x, y longitudes and latitudes if there is 1 or more than 1 :)

panchicore
A: 

Thanks!I want something more. For example x and y are the longitude and latitude i get from my smartphone using javascript. How can i send these values to the server so as to do this searching.I'm stuck in that "SENDING" thing

Display Name
umm.. I need more context, what is the objetive?. write the actions that u wanna do step-by-step of your process to complete the objetive.
panchicore