用户定义的函数引发异常

时间:2018-09-01 10:48:40

标签: python django sqlite

当我在原始查询中使用以下用户定义的函数时,出现以下错误:

  

Django版本:2.1
  异常类型:OperationalError
  异常值:用户定义的函数引发异常

from django.db.backends.signals import connection_created
from django.dispatch import receiver

@receiver(connection_created)
def extend_sqlite(connection=None, **kwargs):
    if connection.vendor == "sqlite":
        # sqlite doesn't natively support math functions, so add them
        cf = connection.connection.create_function
        cf('acos', 1, math.acos)
        cf('cos', 1, math.cos)
        cf('radians', 1, math.radians)
        cf('sin', 1, math.sin)

这是我的原始查询。

query = """SELECT id, (6367*acos(cos(radians(%2f))
                       *cos(radians(lat))*cos(radians(long)-radians(%2f))
                       +sin(radians(%2f))*sin(radians(lat))))
                       AS distance FROM ksApp_outlet
                       group by id having distance < %2f """ % (
                            float(lat),
                            float(lng),
                            float(lat),
                            float(radius)
                        )
            outletdata = Outlet.objects.raw(query)

这是表模式

fields=[
    ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
    ('outletTitle', models.CharField(max_length=100, unique=True)),
    ('address', models.CharField(blank=True, max_length=100, null=True)),
    ('phoneNumber', models.CharField(blank=True, max_length=100, null=True)),
    ('picturesX', models.FileField(blank=True, null=True, upload_to='')),
    ('lat', models.CharField(blank=True, default='foobar', max_length=50, null=True)),
    ('long', models.CharField(blank=True, default=None, max_length=50, null=True)),
    ('region', models.CharField(blank=True, default=None, max_length=50, null=True)),
    ('cuisine', models.ForeignKey(blank=True, null=True, on_delete='CASCADE', to='ksApp.CuisineType')),
    ('merchantName', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='ksApp.Merchant')),
],

不知道我在做什么错。谁能帮我弄清楚这个错误?

0 个答案:

没有答案