def getNearestNotes(request, longitude, latitude):
if request.method == 'GET':
#вызов функции
return HttpResponse('')
else:
return HttpResponse('needGetMethod')from django.db import connection
some_arg = 42
c = connection.cursor()
try:
c.callproc('some_stored_procedure', (some_arg,))
r = c.fetchone()
...
finally:
c.close() def getNearestNotes(request, longitude, latitude):
if request.method == 'GET':
c = connection.cursor()
r = None
try:
c.callproc('GetAllNotes', (longitude, latitude))
r = c.fetchone()
finally:
c.close()
return HttpResponse(str(r))
else:
return HttpResponse('needGetMethod')ОШИБКА: функция getallnotes(unknown, unknown) не существует
LINE 1: SELECT * FROM GetAllNotes('28.7','23.2')
^
HINT: Функция с данными именем и типами аргументов не найдена. Возможно, вам следует добавить явные приведения типов.
create function "GetAllNotes"(long double precision, lat double precision) returns TABLE(userid integer, username character varying, notename character varying, notelong double precision, notelat double precision)
language plpgsql
as
$$
BEGIN
RETURN query (SELECT notes."userid", users."name", notes."name", notes."longitude", notes."latitude" FROM notes INNER JOIN users ON notes."userid" = users."id" WHERE (point(long, lat) <@> point(notes."longitude", notes."latitude") <= 0.124274) );
END
$$; c.callproc('GetAllNotes', (float(longitude), float(latitude))) c.execute('select * from GetAllNotes(%s::double, %s::double)', (longitude, latitude)) ОШИБКА: тип "double" не существует
LINE 1: select * from GetAllNotes('28.7'::double, '23.2'::double)
double precision после двоеточия. def getNearestNotes(request, longitude, latitude):
if request.method == 'GET':
c = connection.cursor()
r = None
try:
#c.callproc('GetAllNotes', (float(longitude), float(latitude)))
c.execute("SELECT routine_name FROM information_schema.routines WHERE routine_type='FUNCTION' AND specific_schema='public'")
r = c.fetchone()
finally:
c.close()
return HttpResponse(str(r))
else:
return HttpResponse('needGetMethod')('earth_distance',), хотя у меня их больше 20 точно. Но моя одна. Все остальные (и earth_distance) создались, когда extentions добавил.
fetchone возвращает только один результат, как следует из его названия. Надо заменить на fetchall, если надо много. Во-вторых, ошибки возникают из-за того, что не совпадают типы передаваемых параметром с ожидаемыми. c.callproc('"GetAllNotes"', (float(longitude), float(latitude))) - вот так работает. Получаю вот такой результат - "[(1, 'Max', 'АВ', Decimal('55.8694248'), Decimal('37.6647453')), (2, 'Nick', 'afsd', Decimal('55.8691788'), Decimal('37.6650843')), (3, 'Jason', 'выаф', Decimal('55.8697598'), Decimal('37.6647674'))]". Вы случайно не знаете как конвертировать это в json. Пытаюсь вот таким способом:r = json.load(c.fetchall())'list' object has no attribute 'read'
import json
from decimal import Decimal
def to_json(obj):
if isinstance(obj, Decimal):
return float(obj)
return str(obj)
filed_names = ['a', 'b', 'c', 'd', 'e']
data = [(1, 'Max', 'АВ', Decimal('55.8694248'), Decimal('37.6647453')), (2, 'Nick', 'afsd', Decimal('55.8691788'), Decimal('37.6650843')), (3, 'Jason', 'выаф', Decimal('55.8697598'), Decimal('37.6647674'))]
json_str = json.dumps([dict(zip(field_names, i)) for i in data)], ensure_ascii=False, default=to_json) c.callproc('GetAllNotes', (float(longitude), float(latitude)))- преобразует переменные во float. А есть какой ни будь аналог, чтобы в varchar(postgres тип данных) конвертировать?
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
SELECT pg_typeof('https://i.imgur.com/DPalXFO.jpg'); --unknown
SELECT pg_typeof('https://i.imgur.com/DPalXFO.jpg'::varchar); --character varyingc.callproc('add_image', (str(uploadet_link), str(request.user.password))), но не помогло. Или как гулить такое? По запросу No function matches the given name and argument types. You might need to add explicit type casts djangoничего не нашел.
c.execute('select * from add_imgae(%s::varchar, %s::varchar)', (uploadet_link, request.user.password)), как вы раньше писали, но не помогло.