django 错误,关于 django-sphinx

发布于 2024-08-19 15:45:26 字数 1999 浏览 4 评论 0原文

from django.db import models
from djangosphinx.models import SphinxSearch

class MyModel(models.Model):
    search = SphinxSearch() # optional: defaults to db_table
    # If your index name does not match MyModel._meta.db_table
    # Note: You can only generate automatic configurations from the ./manage.py script
    # if your index name matches.
    search = SphinxSearch('index_name')

    # Or maybe we want to be more.. specific
    searchdelta = SphinxSearch(
        index='index_name delta_name',
        weights={
            'name': 100,
            'description': 10,
            'tags': 80,
        },
        mode='SPH_MATCH_ALL',
        rankmode='SPH_RANK_NONE',
    )

queryset = MyModel.search.query('query')
results1 = queryset.order_by('@weight', '@id', 'my_attribute')
results2 = queryset.filter(my_attribute=5)
results3 = queryset.filter(my_other_attribute=[5, 3,4])
results4 = queryset.exclude(my_attribute=5)[0:10]
results5 = queryset.count()

# as of 2.0 you can now access an attribute to get the weight and similar arguments
for result in results1:
    print result, result._sphinx
# you can also access a similar set of meta data on the queryset itself (once it's been sliced or executed in any way)
print results1._sphinx

Traceback (most recent call last):
  File "D:\zjm_code\sphinx_test\models.py", line 1, in <module>
    from django.db import models
  File "D:\Python25\Lib\site-packages\django\db\__init__.py", line 10, in <module>
    if not settings.DATABASE_ENGINE:
  File "D:\Python25\Lib\site-packages\django\utils\functional.py", line 269, in __getattr__
    self._setup()
  File "D:\Python25\Lib\site-packages\django\conf\__init__.py", line 38, in _setup
    raise ImportError("Settings cannot be imported, because environment variable %s is undefined." % ENVIRONMENT_VARIABLE)
ImportError: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined.
from django.db import models
from djangosphinx.models import SphinxSearch

class MyModel(models.Model):
    search = SphinxSearch() # optional: defaults to db_table
    # If your index name does not match MyModel._meta.db_table
    # Note: You can only generate automatic configurations from the ./manage.py script
    # if your index name matches.
    search = SphinxSearch('index_name')

    # Or maybe we want to be more.. specific
    searchdelta = SphinxSearch(
        index='index_name delta_name',
        weights={
            'name': 100,
            'description': 10,
            'tags': 80,
        },
        mode='SPH_MATCH_ALL',
        rankmode='SPH_RANK_NONE',
    )

queryset = MyModel.search.query('query')
results1 = queryset.order_by('@weight', '@id', 'my_attribute')
results2 = queryset.filter(my_attribute=5)
results3 = queryset.filter(my_other_attribute=[5, 3,4])
results4 = queryset.exclude(my_attribute=5)[0:10]
results5 = queryset.count()

# as of 2.0 you can now access an attribute to get the weight and similar arguments
for result in results1:
    print result, result._sphinx
# you can also access a similar set of meta data on the queryset itself (once it's been sliced or executed in any way)
print results1._sphinx

and

Traceback (most recent call last):
  File "D:\zjm_code\sphinx_test\models.py", line 1, in <module>
    from django.db import models
  File "D:\Python25\Lib\site-packages\django\db\__init__.py", line 10, in <module>
    if not settings.DATABASE_ENGINE:
  File "D:\Python25\Lib\site-packages\django\utils\functional.py", line 269, in __getattr__
    self._setup()
  File "D:\Python25\Lib\site-packages\django\conf\__init__.py", line 38, in _setup
    raise ImportError("Settings cannot be imported, because environment variable %s is undefined." % ENVIRONMENT_VARIABLE)
ImportError: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

揽清风入怀 2024-08-26 15:45:26

我知道问题是什么:)

您正在尝试独立运行此脚本。但既然你使用 django 模型。所有这些都必须导入到您的命名空间中。这就是错误所在。显然是ImportError

要解决 - 转到您的 django-project 目录 &然后输入python manage.py shell。这将导入所有 Django-env 文件。现在导入您的模型和尝试一下搜索。

I know what the problem is :)

You are trying to run this script as stand-alone. But since you use django models. all of them have to be imported in your namespace. This is what is the error. It clearly days ImportError.

To solve - go to your django-project directory & then type python manage.py shell. This imports all Django-env files. Now import your models & try out the search.

趁年轻赶紧闹 2024-08-26 15:45:26

该错误与环境有关。 Django 找不到您的设置文件;你是在做

python manage.py runserver

本身,还是其他什么?

The error is about the environment. Django cannot find your settings file; Are you doing

python manage.py runserver

itself, or something else?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文