标签归档:Django

不可JSON序列化

问题:不可JSON序列化

我有以下代码序列化查询集;

def render_to_response(self, context, **response_kwargs):

    return HttpResponse(json.simplejson.dumps(list(self.get_queryset())),
                        mimetype="application/json")

以下是我的 get_querset()

[{'product': <Product: hederello ()>, u'_id': u'9802', u'_source': {u'code': u'23981', u'facilities': [{u'facility': {u'name': {u'fr': u'G\xe9n\xe9ral', u'en': u'General'}, u'value': {u'fr': [u'bar', u'r\xe9ception ouverte 24h/24', u'chambres non-fumeurs', u'chambres familiales',.........]}]

我需要序列化。但是它说无法序列化<Product: hederello ()>。因为列表由Django对象和字典组成。有任何想法吗 ?

I have the following code for serializing the queryset;

def render_to_response(self, context, **response_kwargs):

    return HttpResponse(json.simplejson.dumps(list(self.get_queryset())),
                        mimetype="application/json")

And following is my get_querset()

[{'product': <Product: hederello ()>, u'_id': u'9802', u'_source': {u'code': u'23981', u'facilities': [{u'facility': {u'name': {u'fr': u'G\xe9n\xe9ral', u'en': u'General'}, u'value': {u'fr': [u'bar', u'r\xe9ception ouverte 24h/24', u'chambres non-fumeurs', u'chambres familiales',.........]}]

Which I need to serialize. But it says not able to serialize the <Product: hederello ()>. Because list composed of both django objects and dicts. Any ideas ?


回答 0

simplejson并且json不能很好地与Django对象配合使用。

Django的内置序列化器只能序列化由django对象填充的查询集:

data = serializers.serialize('json', self.get_queryset())
return HttpResponse(data, content_type="application/json")

就您而言,self.get_queryset()其中包含django对象和dict的混合。

一种选择是摆脱中的模型实例,self.get_queryset()并使用dict将其替换为model_to_dict

from django.forms.models import model_to_dict

data = self.get_queryset()

for item in data:
   item['product'] = model_to_dict(item['product'])

return HttpResponse(json.simplejson.dumps(data), mimetype="application/json")

希望能有所帮助。

simplejson and json don’t work with django objects well.

Django’s built-in serializers can only serialize querysets filled with django objects:

data = serializers.serialize('json', self.get_queryset())
return HttpResponse(data, content_type="application/json")

In your case, self.get_queryset() contains a mix of django objects and dicts inside.

One option is to get rid of model instances in the self.get_queryset() and replace them with dicts using model_to_dict:

from django.forms.models import model_to_dict

data = self.get_queryset()

for item in data:
   item['product'] = model_to_dict(item['product'])

return HttpResponse(json.simplejson.dumps(data), mimetype="application/json")

Hope that helps.


回答 1

最简单的方法是使用JsonResponse

对于查询集,您应传递该查询集的的列表values,如下所示:

from django.http import JsonResponse

queryset = YourModel.objects.filter(some__filter="some value").values()
return JsonResponse({"models_to_return": list(queryset)})

The easiest way is to use a JsonResponse.

For a queryset, you should pass a list of the the values for that queryset, like so:

from django.http import JsonResponse

queryset = YourModel.objects.filter(some__filter="some value").values()
return JsonResponse({"models_to_return": list(queryset)})

回答 2

我发现可以使用“ .values”方法相当简单地完成此操作,该方法还提供了命名字段:

result_list = list(my_queryset.values('first_named_field', 'second_named_field'))
return HttpResponse(json.dumps(result_list))

必须使用“列表”来获取可迭代的数据,因为“值查询集”类型仅当作为可迭代的拾取时才是字典。

文档:https : //docs.djangoproject.com/en/1.7/ref/models/querysets/#values

I found that this can be done rather simple using the “.values” method, which also gives named fields:

result_list = list(my_queryset.values('first_named_field', 'second_named_field'))
return HttpResponse(json.dumps(result_list))

“list” must be used to get data as iterable, since the “value queryset” type is only a dict if picked up as an iterable.

Documentation: https://docs.djangoproject.com/en/1.7/ref/models/querysets/#values


回答 3

从1.9版本开始,更轻松和官方的获取json的方式

from django.http import JsonResponse
from django.forms.models import model_to_dict


return JsonResponse(  model_to_dict(modelinstance) )

From version 1.9 Easier and official way of getting json

from django.http import JsonResponse
from django.forms.models import model_to_dict


return JsonResponse(  model_to_dict(modelinstance) )

回答 4

我们的js程序员要求我向她返回确切的JSON格式数据,而不是json编码的字符串。

下面是解决方案(这将返回一个可以在浏览器中直接使用/查看的对象)

import json
from xxx.models import alert
from django.core import serializers

def test(request):
    alert_list = alert.objects.all()

    tmpJson = serializers.serialize("json",alert_list)
    tmpObj = json.loads(tmpJson)

    return HttpResponse(json.dumps(tmpObj))

Our js-programmer asked me to return the exact JSON format data instead of a json-encoded string to her.

Below is the solution.(This will return an object that can be used/viewed straightly in the browser)

import json
from xxx.models import alert
from django.core import serializers

def test(request):
    alert_list = alert.objects.all()

    tmpJson = serializers.serialize("json",alert_list)
    tmpObj = json.loads(tmpJson)

    return HttpResponse(json.dumps(tmpObj))

回答 5

首先,我在模型中添加了to_dict方法;

def to_dict(self):
    return {"name": self.woo, "title": self.foo}

然后我有这个;

class DjangoJSONEncoder(JSONEncoder):

    def default(self, obj):
        if isinstance(obj, models.Model):
            return obj.to_dict()
        return JSONEncoder.default(self, obj)


dumps = curry(dumps, cls=DjangoJSONEncoder)

最后使用此类来序列化我的查询集。

def render_to_response(self, context, **response_kwargs):
    return HttpResponse(dumps(self.get_queryset()))

这个效果很好

First I added a to_dict method to my model ;

def to_dict(self):
    return {"name": self.woo, "title": self.foo}

Then I have this;

class DjangoJSONEncoder(JSONEncoder):

    def default(self, obj):
        if isinstance(obj, models.Model):
            return obj.to_dict()
        return JSONEncoder.default(self, obj)


dumps = curry(dumps, cls=DjangoJSONEncoder)

and at last use this class to serialize my queryset.

def render_to_response(self, context, **response_kwargs):
    return HttpResponse(dumps(self.get_queryset()))

This works quite well


Django:配置不正确:SECRET_KEY设置不得为空

问题:Django:配置不正确:SECRET_KEY设置不得为空

我正在尝试设置包括某些基本设置的多个设置文件(开发,生产等)。虽然无法成功。当我尝试运行./manage.py runserver时,出现以下错误:

(cb)clime@den /srv/www/cb $ ./manage.py runserver
ImproperlyConfigured: The SECRET_KEY setting must not be empty.

这是我的设置模块:

(cb)clime@den /srv/www/cb/cb/settings $ ll
total 24
-rw-rw-r--. 1 clime clime 8230 Oct  2 02:56 base.py
-rw-rw-r--. 1 clime clime  489 Oct  2 03:09 development.py
-rw-rw-r--. 1 clime clime   24 Oct  2 02:34 __init__.py
-rw-rw-r--. 1 clime clime  471 Oct  2 02:51 production.py

基本设置(包含SECRET_KEY):

(cb)clime@den /srv/www/cb/cb/settings $ cat base.py:
# Django base settings for cb project.

import django.conf.global_settings as defaults

DEBUG = False
TEMPLATE_DEBUG = False

INTERNAL_IPS = ('127.0.0.1',)

ADMINS = (
    ('clime', 'clime7@gmail.com'),
)

MANAGERS = ADMINS

DATABASES = {
    'default': {
        #'ENGINE': 'django.db.backends.postgresql_psycopg2', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'cwu',                   # Or path to database file if using sqlite3.
        'USER': 'clime',                 # Not used with sqlite3.
        'PASSWORD': '',                  # Not used with sqlite3.
        'HOST': '',                      # Set to empty string for localhost. Not used with sqlite3.
        'PORT': '',                      # Set to empty string for default. Not used with sqlite3.
    }
}

# Local time zone for this installation. Choices can be found here:
# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
# although not all choices may be available on all operating systems.
# In a Windows environment this must be set to your system time zone.
TIME_ZONE = 'Europe/Prague'

# Language code for this installation. All choices can be found here:
# http://www.i18nguy.com/unicode/language-identifiers.html
LANGUAGE_CODE = 'en-us'

SITE_ID = 1

# If you set this to False, Django will make some optimizations so as not
# to load the internationalization machinery.
USE_I18N = False

# If you set this to False, Django will not format dates, numbers and
# calendars according to the current locale.
USE_L10N = False # TODO: make this true and accustom date time input

DATE_INPUT_FORMATS = defaults.DATE_INPUT_FORMATS + ('%d %b %y', '%d %b, %y') # + ('25 Oct 13', '25 Oct, 13')

# If you set this to False, Django will not use timezone-aware datetimes.
USE_TZ = True

# Absolute filesystem path to the directory that will hold user-uploaded files.
# Example: "/home/media/media.lawrence.com/media/"
MEDIA_ROOT = '/srv/www/cb/media'

# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash.
# Examples: "http://media.lawrence.com/media/", "http://example.com/media/"
MEDIA_URL = '/media/'

# Absolute path to the directory static files should be collected to.
# Don't put anything in this directory yourself; store your static files
# in apps' "static/" subdirectories and in STATICFILES_DIRS.
# Example: "/home/media/media.lawrence.com/static/"
STATIC_ROOT = '/srv/www/cb/static'

# URL prefix for static files.
# Example: "http://media.lawrence.com/static/"
STATIC_URL = '/static/'

# Additional locations of static files
STATICFILES_DIRS = (
    # Put strings here, like "/home/html/static" or "C:/www/django/static".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
)

# List of finder classes that know how to find static files in
# various locations.
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
#    'django.contrib.staticfiles.finders.DefaultStorageFinder',
)

# Make this unique, and don't share it with anybody.
SECRET_KEY = '8lu*6g0lg)9z!ba+a$ehk)xt)x%rxgb$i1&amp;022shmi1jcgihb*'

# List of callables that know how to import templates from various sources.
TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
#     'django.template.loaders.eggs.Loader',
)

TEMPLATE_CONTEXT_PROCESSORS = (
    'django.contrib.auth.context_processors.auth',
    'django.core.context_processors.request',
    'django.core.context_processors.debug',
    'django.core.context_processors.i18n',
    'django.core.context_processors.media',
    'django.core.context_processors.static',
    'django.core.context_processors.tz',
    'django.contrib.messages.context_processors.messages',
    'web.context.inbox',
    'web.context.base',
    'web.context.main_search',
    'web.context.enums',
)

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'watson.middleware.SearchContextMiddleware',
    'debug_toolbar.middleware.DebugToolbarMiddleware',
    'middleware.UserMemberMiddleware',
    'middleware.ProfilerMiddleware',
    'middleware.VaryOnAcceptMiddleware',
    # Uncomment the next line for simple clickjacking protection:
    # 'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

ROOT_URLCONF = 'cb.urls'

# Python dotted path to the WSGI application used by Django's runserver.
WSGI_APPLICATION = 'cb.wsgi.application'

TEMPLATE_DIRS = (
    # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
    '/srv/www/cb/web/templates',
    '/srv/www/cb/templates',
)

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'south',
    'grappelli', # must be before admin
    'django.contrib.admin',
    'django.contrib.admindocs',
    'endless_pagination',
    'debug_toolbar',
    'djangoratings',
    'watson',
    'web',
)

AUTH_USER_MODEL = 'web.User'

# A sample logging configuration. The only tangible logging
# performed by this configuration is to send an email to
# the site admins on every HTTP 500 error when DEBUG=False.
# See http://docs.djangoproject.com/en/dev/topics/logging for
# more details on how to customize your logging configuration.
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'filters': {
        'require_debug_false': {
            '()': 'django.utils.log.RequireDebugFalse'
        }
    },
    'formatters': {
        'standard': {
            'format' : "[%(asctime)s] %(levelname)s [%(name)s:%(lineno)s] %(message)s",
            'datefmt' : "%d/%b/%Y %H:%M:%S"
        },
    },
    'handlers': {
        'mail_admins': {
            'level': 'ERROR',
            'filters': ['require_debug_false'],
            'class': 'django.utils.log.AdminEmailHandler'
        },
        'null': {
            'level':'DEBUG',
            'class':'django.utils.log.NullHandler',
        },
        'logfile': {
            'level':'DEBUG',
            'class':'logging.handlers.RotatingFileHandler',
            'filename': "/srv/www/cb/logs/application.log",
            'maxBytes': 50000,
            'backupCount': 2,
            'formatter': 'standard',
        },
        'console':{
            'level':'INFO',
            'class':'logging.StreamHandler',
            'formatter': 'standard'
        },
    },
    'loggers': {
        'django.request': {
            'handlers': ['mail_admins'],
            'level': 'ERROR',
            'propagate': True,
        },
        'django': {
            'handlers':['console'],
            'propagate': True,
            'level':'WARN',
        },
        'django.db.backends': {
            'handlers': ['console'],
            'level': 'DEBUG',
            'propagate': False,
        },
        'web': {
            'handlers': ['console', 'logfile'],
            'level': 'DEBUG',
        },
    },
}

LOGIN_URL = 'login'
LOGOUT_URL = 'logout'

#ENDLESS_PAGINATION_LOADING = """
#    <img src="/static/web/img/preloader.gif" alt="loading" style="margin:auto"/>
#"""
ENDLESS_PAGINATION_LOADING = """
    <div class="spinner small" style="margin:auto">
        <div class="block_1 spinner_block small"></div>
        <div class="block_2 spinner_block small"></div>
        <div class="block_3 spinner_block small"></div>
    </div>
"""

DEBUG_TOOLBAR_CONFIG = {
    'INTERCEPT_REDIRECTS': False,
}

import django.template.loader
django.template.loader.add_to_builtins('web.templatetags.cb_tags')
django.template.loader.add_to_builtins('web.templatetags.tag_library')

WATSON_POSTGRESQL_SEARCH_CONFIG = 'public.english_nostop'

设置文件之一:

(cb)clime@den /srv/www/cb/cb/settings $ cat development.py 
from base import *

DEBUG = True
TEMPLATE_DEBUG = True

ALLOWED_HOSTS = ['127.0.0.1', '31.31.78.149']

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'cwu',
        'USER': 'clime',
        'PASSWORD': '',
        'HOST': '',
        'PORT': '',
    }
}

MEDIA_ROOT = '/srv/www/cb/media/'

STATIC_ROOT = '/srv/www/cb/static/'

TEMPLATE_DIRS = (
    '/srv/www/cb/web/templates',
    '/srv/www/cb/templates',
)

代码在manage.py

(cb)clime@den /srv/www/cb $ cat manage.py 
#!/usr/bin/env python
import os
import sys

if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "cb.settings.development")

    from django.core.management import execute_from_command_line

    execute_from_command_line(sys.argv)

如果添加from base import */srv/www/cb/cb/settings/__init__.py(否则为空),它会神奇地开始工作,但我不明白为什么。任何人都可以向我解释这是怎么回事?它一定是一些python模块魔术。

编辑:如果我从base.py中删除此行,一切也将开始工作

django.template.loader.add_to_builtins('web.templatetags.cb_tags')

如果我从web.templatetags.cb_tags中删除此行,它也将开始工作:

from endless_pagination.templatetags import endless

我想这是因为最终导致

from django.conf import settings
PER_PAGE = getattr(settings, 'ENDLESS_PAGINATION_PER_PAGE', 10)

因此,它会产生一些怪异的循环内容并结束游戏。

I am trying to set up multiple setting files (development, production, ..) that include some base settings. Cannot succeed though. When I try to run ./manage.py runserver I am getting the following error:

(cb)clime@den /srv/www/cb $ ./manage.py runserver
ImproperlyConfigured: The SECRET_KEY setting must not be empty.

Here is my settings module:

(cb)clime@den /srv/www/cb/cb/settings $ ll
total 24
-rw-rw-r--. 1 clime clime 8230 Oct  2 02:56 base.py
-rw-rw-r--. 1 clime clime  489 Oct  2 03:09 development.py
-rw-rw-r--. 1 clime clime   24 Oct  2 02:34 __init__.py
-rw-rw-r--. 1 clime clime  471 Oct  2 02:51 production.py

Base settings (contain SECRET_KEY):

(cb)clime@den /srv/www/cb/cb/settings $ cat base.py:
# Django base settings for cb project.

import django.conf.global_settings as defaults

DEBUG = False
TEMPLATE_DEBUG = False

INTERNAL_IPS = ('127.0.0.1',)

ADMINS = (
    ('clime', 'clime7@gmail.com'),
)

MANAGERS = ADMINS

DATABASES = {
    'default': {
        #'ENGINE': 'django.db.backends.postgresql_psycopg2', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'cwu',                   # Or path to database file if using sqlite3.
        'USER': 'clime',                 # Not used with sqlite3.
        'PASSWORD': '',                  # Not used with sqlite3.
        'HOST': '',                      # Set to empty string for localhost. Not used with sqlite3.
        'PORT': '',                      # Set to empty string for default. Not used with sqlite3.
    }
}

# Local time zone for this installation. Choices can be found here:
# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
# although not all choices may be available on all operating systems.
# In a Windows environment this must be set to your system time zone.
TIME_ZONE = 'Europe/Prague'

# Language code for this installation. All choices can be found here:
# http://www.i18nguy.com/unicode/language-identifiers.html
LANGUAGE_CODE = 'en-us'

SITE_ID = 1

# If you set this to False, Django will make some optimizations so as not
# to load the internationalization machinery.
USE_I18N = False

# If you set this to False, Django will not format dates, numbers and
# calendars according to the current locale.
USE_L10N = False # TODO: make this true and accustom date time input

DATE_INPUT_FORMATS = defaults.DATE_INPUT_FORMATS + ('%d %b %y', '%d %b, %y') # + ('25 Oct 13', '25 Oct, 13')

# If you set this to False, Django will not use timezone-aware datetimes.
USE_TZ = True

# Absolute filesystem path to the directory that will hold user-uploaded files.
# Example: "/home/media/media.lawrence.com/media/"
MEDIA_ROOT = '/srv/www/cb/media'

# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash.
# Examples: "http://media.lawrence.com/media/", "http://example.com/media/"
MEDIA_URL = '/media/'

# Absolute path to the directory static files should be collected to.
# Don't put anything in this directory yourself; store your static files
# in apps' "static/" subdirectories and in STATICFILES_DIRS.
# Example: "/home/media/media.lawrence.com/static/"
STATIC_ROOT = '/srv/www/cb/static'

# URL prefix for static files.
# Example: "http://media.lawrence.com/static/"
STATIC_URL = '/static/'

# Additional locations of static files
STATICFILES_DIRS = (
    # Put strings here, like "/home/html/static" or "C:/www/django/static".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
)

# List of finder classes that know how to find static files in
# various locations.
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
#    'django.contrib.staticfiles.finders.DefaultStorageFinder',
)

# Make this unique, and don't share it with anybody.
SECRET_KEY = '8lu*6g0lg)9z!ba+a$ehk)xt)x%rxgb$i1&amp;022shmi1jcgihb*'

# List of callables that know how to import templates from various sources.
TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
#     'django.template.loaders.eggs.Loader',
)

TEMPLATE_CONTEXT_PROCESSORS = (
    'django.contrib.auth.context_processors.auth',
    'django.core.context_processors.request',
    'django.core.context_processors.debug',
    'django.core.context_processors.i18n',
    'django.core.context_processors.media',
    'django.core.context_processors.static',
    'django.core.context_processors.tz',
    'django.contrib.messages.context_processors.messages',
    'web.context.inbox',
    'web.context.base',
    'web.context.main_search',
    'web.context.enums',
)

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'watson.middleware.SearchContextMiddleware',
    'debug_toolbar.middleware.DebugToolbarMiddleware',
    'middleware.UserMemberMiddleware',
    'middleware.ProfilerMiddleware',
    'middleware.VaryOnAcceptMiddleware',
    # Uncomment the next line for simple clickjacking protection:
    # 'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

ROOT_URLCONF = 'cb.urls'

# Python dotted path to the WSGI application used by Django's runserver.
WSGI_APPLICATION = 'cb.wsgi.application'

TEMPLATE_DIRS = (
    # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
    '/srv/www/cb/web/templates',
    '/srv/www/cb/templates',
)

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'south',
    'grappelli', # must be before admin
    'django.contrib.admin',
    'django.contrib.admindocs',
    'endless_pagination',
    'debug_toolbar',
    'djangoratings',
    'watson',
    'web',
)

AUTH_USER_MODEL = 'web.User'

# A sample logging configuration. The only tangible logging
# performed by this configuration is to send an email to
# the site admins on every HTTP 500 error when DEBUG=False.
# See http://docs.djangoproject.com/en/dev/topics/logging for
# more details on how to customize your logging configuration.
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'filters': {
        'require_debug_false': {
            '()': 'django.utils.log.RequireDebugFalse'
        }
    },
    'formatters': {
        'standard': {
            'format' : "[%(asctime)s] %(levelname)s [%(name)s:%(lineno)s] %(message)s",
            'datefmt' : "%d/%b/%Y %H:%M:%S"
        },
    },
    'handlers': {
        'mail_admins': {
            'level': 'ERROR',
            'filters': ['require_debug_false'],
            'class': 'django.utils.log.AdminEmailHandler'
        },
        'null': {
            'level':'DEBUG',
            'class':'django.utils.log.NullHandler',
        },
        'logfile': {
            'level':'DEBUG',
            'class':'logging.handlers.RotatingFileHandler',
            'filename': "/srv/www/cb/logs/application.log",
            'maxBytes': 50000,
            'backupCount': 2,
            'formatter': 'standard',
        },
        'console':{
            'level':'INFO',
            'class':'logging.StreamHandler',
            'formatter': 'standard'
        },
    },
    'loggers': {
        'django.request': {
            'handlers': ['mail_admins'],
            'level': 'ERROR',
            'propagate': True,
        },
        'django': {
            'handlers':['console'],
            'propagate': True,
            'level':'WARN',
        },
        'django.db.backends': {
            'handlers': ['console'],
            'level': 'DEBUG',
            'propagate': False,
        },
        'web': {
            'handlers': ['console', 'logfile'],
            'level': 'DEBUG',
        },
    },
}

LOGIN_URL = 'login'
LOGOUT_URL = 'logout'

#ENDLESS_PAGINATION_LOADING = """
#    <img src="/static/web/img/preloader.gif" alt="loading" style="margin:auto"/>
#"""
ENDLESS_PAGINATION_LOADING = """
    <div class="spinner small" style="margin:auto">
        <div class="block_1 spinner_block small"></div>
        <div class="block_2 spinner_block small"></div>
        <div class="block_3 spinner_block small"></div>
    </div>
"""

DEBUG_TOOLBAR_CONFIG = {
    'INTERCEPT_REDIRECTS': False,
}

import django.template.loader
django.template.loader.add_to_builtins('web.templatetags.cb_tags')
django.template.loader.add_to_builtins('web.templatetags.tag_library')

WATSON_POSTGRESQL_SEARCH_CONFIG = 'public.english_nostop'

One of the setting files:

(cb)clime@den /srv/www/cb/cb/settings $ cat development.py 
from base import *

DEBUG = True
TEMPLATE_DEBUG = True

ALLOWED_HOSTS = ['127.0.0.1', '31.31.78.149']

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'cwu',
        'USER': 'clime',
        'PASSWORD': '',
        'HOST': '',
        'PORT': '',
    }
}

MEDIA_ROOT = '/srv/www/cb/media/'

STATIC_ROOT = '/srv/www/cb/static/'

TEMPLATE_DIRS = (
    '/srv/www/cb/web/templates',
    '/srv/www/cb/templates',
)

Code in manage.py:

(cb)clime@den /srv/www/cb $ cat manage.py 
#!/usr/bin/env python
import os
import sys

if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "cb.settings.development")

    from django.core.management import execute_from_command_line

    execute_from_command_line(sys.argv)

If I add from base import * into /srv/www/cb/cb/settings/__init__.py (which is otherwise empty), it magically starts to work but I don’t understand why. Anyone could explain to me what’s going on here? It must be some python module magic.

EDIT: Everything also starts to work if I remove this line from base.py

django.template.loader.add_to_builtins('web.templatetags.cb_tags')

If I remove this line from web.templatetags.cb_tags, it also starts to work:

from endless_pagination.templatetags import endless

I guess it is because, in the end, it leads to

from django.conf import settings
PER_PAGE = getattr(settings, 'ENDLESS_PAGINATION_PER_PAGE', 10)

So it creates some weird circular stuff and game over.


回答 0

我有同样的错误,结果是设置或设置模块本身加载的模块或类之间存在循环依赖关系。在我的情况下,这是一个中间件类,该类在设置中被命名,该类本身试图加载设置。

I had the same error and it turned out to be a circular dependency between a module or class loaded by the settings and the settings module itself. In my case it was a middleware class which was named in the settings which itself tried to load the settings.


回答 1

在按照Daniel Greenfield的《Django两个独家报道》一书中的说明重组设置后,我遇到了同样的问题。

我通过设置解决了该问题

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project_name.settings.local")

manage.pywsgi.py

更新:

在上述解决方案中,local是我的settings文件夹中的文件名(settings / local.py),其中包含我的本地环境的设置。

解决此问题的另一种方法是将所有常用设置保留在settings / base.py中,然后为生产,登台和开发环境创建3个单独的设置文件。

您的设置文件夹如下所示:

settings/
    __init__.py
    base.py
    local.py
    prod.py
    stage.py

并将以下代码保存在 settings/__init__.py

from .base import *

env_name = os.getenv('ENV_NAME', 'local')

if env_name == 'prod':
    from .prod import *
elif env_name == 'stage':
    from .stage import *
else:
    from .local import *

I ran into the same problem after restructuring the settings as per the instructions from Daniel Greenfield’s book Two scoops of Django.

I resolved the issue by setting

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project_name.settings.local")

in manage.py and wsgi.py.

Update:

In the above solution, local is the file name (settings/local.py) inside my settings folder, which holds the settings for my local environment.

Another way to resolve this issue is to keep all your common settings inside settings/base.py and then create 3 separate settings files for your production, staging and dev environments.

Your settings folder will look like:

settings/
    __init__.py
    base.py
    local.py
    prod.py
    stage.py

and keep the following code in your settings/__init__.py

from .base import *

env_name = os.getenv('ENV_NAME', 'local')

if env_name == 'prod':
    from .prod import *
elif env_name == 'stage':
    from .stage import *
else:
    from .local import *

回答 2

我有同样的错误 python manage.py runserver

对我来说,这是由于过时的已编译二进制(.pyc)文件所致。删除项目中的所有此类文件后,服​​务器再次开始运行。:)

因此,如果您无所事事地收到此错误,即不进行与Django设置有关的任何更改,那么这可能是个不错的选择。

I had the same error with python manage.py runserver.

For me, it turned out that it was because of a stale compiled binary (.pyc) file. After deleting all such files in my project, server started running again. :)

So if you get this error, out of nowhere, i.e without making any change seemingly-related to django-settings, this could be a good first measure.


回答 3

删除.pyc文件

Ubuntu终端命令用于删除.pyc: find . -name "*.pyc" -exec rm -rf {} \;

我在执行python manage.py runserver时遇到了相同的错误。这是因为.pyc文件。我从项目目录中删除了.pyc文件,然后它开始工作了。

Remove .pyc files

Ubuntu terminal command for deleting .pyc : find . -name "*.pyc" -exec rm -rf {} \;

I have got same error when I did python manage.py runserver. It was because .pyc file. I deleted .pyc file from project directory then it was working.


回答 4

我没有指定设置文件:

python manage.py runserver --settings=my_project.settings.develop

I hadn’t specified the settings file:

python manage.py runserver --settings=my_project.settings.develop

回答 5

它开始工作是因为在base.py上,您具有基本设置文件中所需的所有信息。您需要以下行:

SECRET_KEY = '8lu*6g0lg)9z!ba+a$ehk)xt)x%rxgb$i1&amp;022shmi1jcgihb*'

因此它可以正常工作,并且在您执行操作时from base import *,会将SECRET_KEY导入到您的development.py

在执行任何自定义设置之前,您应该始终导入基本设置。


编辑:另外,当django从程序包中导入开发时,由于您from base import *在内部定义,因此它将初始化base 内部的所有变量__init__.py

It starts working because on the base.py you have all information needed in a basic settings file. You need the line:

SECRET_KEY = '8lu*6g0lg)9z!ba+a$ehk)xt)x%rxgb$i1&amp;022shmi1jcgihb*'

So it works and when you do from base import *, it imports SECRET_KEY into your development.py.

You should always import basic settings before doing any custom settings.


EDIT: Also, when django imports development from your package, it initializes all variables inside base since you defined from base import * inside __init__.py


回答 6

我认为这是环境错误,您应该尝试设置:DJANGO_SETTINGS_MODULE='correctly_settings'

I think that it is the Environment error,you should try setting : DJANGO_SETTINGS_MODULE='correctly_settings'


回答 7

我在Celery上也遇到了同样的问题。我的setting.py 之前

SECRET_KEY = os.environ.get('DJANGO_SECRET_KEY')

后:

SECRET_KEY = os.environ.get('DJANGO_SECRET_KEY', <YOUR developing key>)

如果未定义环境变量,则:SECRET_KEY = 您的开发密钥

I had the same problem with Celery. My setting.py before:

SECRET_KEY = os.environ.get('DJANGO_SECRET_KEY')

after:

SECRET_KEY = os.environ.get('DJANGO_SECRET_KEY', <YOUR developing key>)

If the environment variables are not defined then: SECRET_KEY = YOUR developing key


回答 8

在设置目录的init .py中,输入正确的导入,例如:

from Project.settings.base import *

无需更改wsgi.py或manage.py

In the init.py of the settings directory write the correct import, like:

from Project.settings.base import *

No need to change wsgi.py or manage.py


回答 9

我通过停用所有激活到virtualenv的活动会话并再次启动它,解决了在OS X上使用Django 1.5和1.6的OS X上出现的问题。

I solved this problem occurring on OS X with Django both 1.5 and 1.6 by deactivating all active sessions to virtualenv and starting it again.


回答 10

为了将另一个潜在的解决方案添加到组合中,我有一个settings文件夹以及一个settings.py在项目目录中。(我正在从基于环境的设置文件切换回一个文件。此后我一直在考虑。)

Python对我要导入project/settings.py还是感到困惑project/settings/__init__.py。我删除了settings目录,现在一切正常。

To throw another potential solution into the mix, I had a settings folder as well as a settings.py in my project dir. (I was switching back from environment-based settings files to one file. I have since reconsidered.)

Python was getting confused about whether I wanted to import project/settings.py or project/settings/__init__.py. I removed the settings dir and everything now works fine.


回答 11

对于使用PyCharm的任何人:绿色的“运行选定的配置”按钮将产生此错误,但运行以下工作:

py manage.py runserver 127.0.0.1:8000 --settings=app_name.settings.development

要解决此问题,您需要编辑配置的环境变量。为此,请单击绿色运行按钮左侧的“选择运行/调试配置”下拉菜单,然后单击“编辑配置”。在“环境”标签下,将环境变量更改DJANGO_SETTINGS_MODULEapp_name.settings.development

For anyone using PyCharm: the green “Run selected configuration” button would produce this error, yet running the following works:

py manage.py runserver 127.0.0.1:8000 --settings=app_name.settings.development

To fix this you need to edit the configuration’s environment variables. To do this click the “Select run/debug configuration” drop-down menu to the left of the green run button and then click on “edit configuration”. Under the “environment” tab change the environment variable DJANGO_SETTINGS_MODULE to app_name.settings.development.


回答 12

我只是想补充一点,当我的数据库名称在settings.py文件中拼写错误时出现了此错误,因此无法创建数据库。

I just wanted to add that I got this error when my database name was spelled wrong in my settings.py file so the DB couldn’t be created.


回答 13

我通过修复有错字的TEMPLATES设置在1.8.4上解决了此问题(删除TEMPLATES [‘debug’]解决了此问题)

翻阅您最近更改的设置,确保所有键都是按书进行的。

I solved this problem on 1.8.4 by fixing the TEMPLATES settings which had a typo (removing TEMPLATES[‘debug’] solved it)

Go over the settings that you have changed recently, make sure all keys are by-the-book.


回答 14

我通过删除文件中等号(=)周围的空格解决了这个问题.env

I solved this problem by removing the spaces around equal signs (=) in my .env file.


回答 15

在我的情况下,问题是-我有我的app_folder,并settings.py在里面。然后我决定进入Settings folder内部app_folder-并与之发生冲突settings.py。只是重命名了Settings folder-一切正常。

In my case the problem was – I had my app_folder and settings.py in it. Then I decided to make Settings folder inside app_folder – and that made a collision with settings.py. Just renamed that Settings folder – and everything worked.


回答 16

我的Mac OS不喜欢它没有在设置文件中找到env变量集:

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = os.environ.get('MY_SERVER_ENV_VAR_NAME')

但是将env var添加到本地Mac OS开发环境后,该错误消失了:

export MY_SERVER_ENV_VAR_NAME ='fake dev security key that is longer than 50 characters.'

就我而言,我还需要添加--settings参数:

python3 manage.py check --deploy --settings myappname.settings.production

其中production.py是一个包含设置文件夹中特定于生产的设置的文件。

My Mac OS didn’t like that it didn’t find the env variable set in the settings file:

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = os.environ.get('MY_SERVER_ENV_VAR_NAME')

but after adding the env var to my local Mac OS dev environment, the error disappeared:

export MY_SERVER_ENV_VAR_NAME ='fake dev security key that is longer than 50 characters.'

In my case, I also needed to add the --settings param:

python3 manage.py check --deploy --settings myappname.settings.production

where production.py is a file containing production specific settings inside a settings folder.


回答 17

对我来说,问题是要get_text_noop反复使用LANGUAGES。

改变中

LANGUAGES = (
    ('en-gb', get_text_noop('British English')),
    ('fr', get_text_noop('French')),
)

from django.utils.translation import gettext_lazy as _

LANGUAGES = (
    ('en-gb', _('British English')),
    ('fr', _('French')),
)

在基本设置文件中解决了ImproperlyConfigured: The SECRET_KEY setting must not be empty异常。

The issue for me was calling get_text_noop in the LANGUAGES iterable.

Changing

LANGUAGES = (
    ('en-gb', get_text_noop('British English')),
    ('fr', get_text_noop('French')),
)

to

from django.utils.translation import gettext_lazy as _

LANGUAGES = (
    ('en-gb', _('British English')),
    ('fr', _('French')),
)

in the base settings file resolved the ImproperlyConfigured: The SECRET_KEY setting must not be empty exception.


回答 18

我通过在settings.py中的注释行解决了上述问题

SECRET_KEY=os.environ.get('SECRET_KEY')

SECRET_KEY 在我的声明 ~/.bashrc文件中(对于Linux Ubuntu用户)

为了在我的本地计算机上进行开发,我没有使用evironmnet变量

SECRET_KEY = '(i9b4aes#h1)m3h_8jh^duxrdh$4pu8-q5vkba2yf$ptd1lev_'

上面的线没有给出错误

I solved the above problem by commenting the line in my settings.py

SECRET_KEY=os.environ.get('SECRET_KEY')

SECRET_KEY declared in my ~/.bashrc file(for linux Ubuntu users)

For development purpose on my localmachine I did not use evironmnet variable

SECRET_KEY = '(i9b4aes#h1)m3h_8jh^duxrdh$4pu8-q5vkba2yf$ptd1lev_'

above line didn’t give the error


回答 19

就我而言,在设置Github操作时,我只是忘记了将env变量添加到yml文件中:

jobs:
  build:
    env:
     VAR1: 1
     VAR2: 5

In my case, while setting up a Github action I just forgot to add the env variables to the yml file:

jobs:
  build:
    env:
     VAR1: 1
     VAR2: 5

回答 20

答案如此之多的原因是,该异常可能与SECRET_KEY没有任何关系。可能是一个较早的exceptions正在被吞噬。使用DEBUG = True打开调试以查看真正的异常。

The reason why there are so many different answers is because the exception probably doesn’t have anything to do with the SECRET_KEY. It is probably an earlier exception that is being swallowed. Turn on debugging using DEBUG=True to see the real exception.


回答 21

就我而言,经过长时间的搜索,我发现您的Django设置(设置>语言和框架> Django)中的PyCharm具有未定义的配置文件字段。您应该使该字段指向项目的设置文件。然后,您必须打开“运行/调试”设置并删除环境变量DJANGO_SETTINGS_MODULE =现有路径。

发生这种情况是因为PyCharm中的Django插件会强制配置框架。因此,没有必要配置任何os.environ.setdefault(’DJANGO_SETTINGS_MODULE’,’myapp.settings’)

In my case, after a long search I found that PyCharm in your Django settings (Settings > Languages & Frameworks > Django) had the configuration file field undefined. You should make this field point to your project’s settings file. Then, you must open the Run / Debug settings and remove the environment variable DJANGO_SETTINGS_MODULE = existing path.

This happens because the Django plugin in PyCharm forces the configuration of the framework. So there is no point in configuring any os.environ.setdefault(‘DJANGO_SETTINGS_MODULE’, ‘myapp.settings’)


回答 22

导入base.py __init__.py 单独确保您不会再次重复相同的配置!。

设置环境变量 SET DJANGO_DEVELOPMENT =dev

settings/
  __init__.py
  base.py
  local.py
  production.py

__init__.py

from .base import *
if os.environ.get('DJANGO_DEVELOPMENT')=='prod':
   from .production import *
else:
   from .local import *

在已base.py配置的全局配置中。除了数据库。喜欢

SECRET_KEY, ALLOWED_HOSTS,INSTALLED_APPS,MIDDLEWARE .. etc....

local.py

DATABASES = {
'default': {
    'ENGINE': 'django.db.backends.postgresql_psycopg2',
    'NAME': 'database',
    'USER': 'postgres',
    'PASSWORD': 'password',
    'HOST': 'localhost',
    'PORT': '5432',
}
}

Import base.py in __init__.py alone. make sure you won’t repeat the same configuration again!.

set environment variable SET DJANGO_DEVELOPMENT =dev

settings/
  __init__.py
  base.py
  local.py
  production.py

In __init__.py

from .base import *
if os.environ.get('DJANGO_DEVELOPMENT')=='prod':
   from .production import *
else:
   from .local import *

In base.py configured the global configurations. except for Database. like

SECRET_KEY, ALLOWED_HOSTS,INSTALLED_APPS,MIDDLEWARE .. etc....

In local.py

DATABASES = {
'default': {
    'ENGINE': 'django.db.backends.postgresql_psycopg2',
    'NAME': 'database',
    'USER': 'postgres',
    'PASSWORD': 'password',
    'HOST': 'localhost',
    'PORT': '5432',
}
}

回答 23

我来这里时正面临着同样的问题,因此我一直在寻找答案,但是这里没有答案对我有用。然后,在其他网站中搜索后,我偶然发现了这个简单的修复方法。对我有用

wsgi.py

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'yourProject.settings')

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'yourProject.settings.dev')

I came here looking for answer as I was facing the same issues, none of the answers here worked for me. Then after searching in other websites i stumbled upon this simple fix. It worked for me

wsgi.py

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'yourProject.settings')

to

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'yourProject.settings.dev')

django模型选择单个字段

问题:django模型选择单个字段

我有一个称为的表/模型Employees,我想将单个字段的所有行作为查询集。

我知道我可以这样做(希望我做得对):

emp_list = Employees.objects.get(all)
emp_names = emp_list.eng_name

是否要在数据库中查询所有字段并且仅使用一个字段?是否有更好(更快)的方法?

I have a table/models called Employees and I would like to get all rows of a single field as a queryset.

I know I can do it like this (hope I’m doing this right even):

emp_list = Employees.objects.get(all)
emp_names = emp_list.eng_name

Would query the database for all fields and using only one? Is there a better (faster) way of doing this?


回答 0

Employees.objects.values_list('eng_name', flat=True)

这将创建所有eng_names的平面列表。如果您希望每行多于一个字段,则不能做一个平面列表:这将创建一个元组列表:

Employees.objects.values_list('eng_name', 'rank')
Employees.objects.values_list('eng_name', flat=True)

That creates a flat list of all eng_names. If you want more than one field per row, you can’t do a flat list: this will create a list of tuples:

Employees.objects.values_list('eng_name', 'rank')

回答 1

除了values_list丹尼尔 提到你也可以使用only(或defer为相反的效果),只得到有他们的ID和指定的字段对象的查询集:

Employees.objects.only('eng_name')

这将运行一个查询:

SELECT id, eng_name FROM employees

In addition to values_list as Daniel mentions you can also use only (or defer for the opposite effect) to get a queryset of objects only having their id and specified fields:

Employees.objects.only('eng_name')

This will run a single query:

SELECT id, eng_name FROM employees

回答 2

我们可以在值上选择必填字段。

Employee.objects.all().values('eng_name','rank')

We can select required fields over values.

Employee.objects.all().values('eng_name','rank')

回答 3

Oskar Persson的答案是处理该数据的最佳方法,因为当我们获得对象实例(很容易迭代以获取道具)而不是简单的值列表时,可以更轻松地将数据传递到上下文并从模板正常对待数据。

之后,您可以轻松获得所需的道具:

for employee in employees:
    print(employee.eng_name)

或在模板中:

{% for employee in employees %}

    <p>{{ employee.eng_name }}</p>

{% endfor %}

Oskar Persson’s answer is the best way to handle it because makes it easier to pass the data to the context and treat it normally from the template as we get the object instances (easily iterable to get props) instead of a plain value list.

After that you can just easily get the wanted prop:

for employee in employees:
    print(employee.eng_name)

Or in the template:

{% for employee in employees %}

    <p>{{ employee.eng_name }}</p>

{% endfor %}

回答 4

您可以像这样在过滤器旁边使用values_list;

active_emps_first_name = Employees.objects.filter(active=True).values_list('first_name',flat=True)

在这里更多细节

You can use values_list alongside filter like so;

active_emps_first_name = Employees.objects.filter(active=True).values_list('first_name',flat=True)

More details here


何时使用序列化器的create()和ModelViewset的create()perform_create()

问题:何时使用序列化器的create()和ModelViewset的create()perform_create()

我想澄清django-rest-framework有关创建模型对象的给定文档。到目前为止,我发现有3种方法来处理此类事件。

  1. 序列化器的create()方法。这是文档

    class CommentSerializer(serializers.Serializer):
    
        def create(self, validated_data):
            return Comment.objects.create(**validated_data)
    
  2. ModelViewsetcreate()方法。文献资料

    class AccountViewSet(viewsets.ModelViewSet):
    
        queryset = Account.objects.all()
        serializer_class = AccountSerializer
        permission_classes = [IsAccountAdminOrReadOnly]
    
  3. ModelViewsetperform_create()方法。文献资料

    class SnippetViewSet(viewsets.ModelViewSet):
    
        def perform_create(self, serializer):
            serializer.save(owner=self.request.user)
    

这三种方法很重要,具体取决于您的应用程序环境。

但是什么时候我们需要使用每个create() / perform_create()函数?另一方面,我发现有人要求为单个发布请求调用modelviewsetcreate()和serializer的两个create方法create()

希望任何人都可以分享他们的一些知识来进行解释,这肯定会对我的开发过程有所帮助。

I want to clarify the given documentation of django-rest-framework regarding the creation of a model object. So far I found that there are 3 approaches on how to handle such events.

  1. The Serializer’s create() method. Here is the documentation

    class CommentSerializer(serializers.Serializer):
    
        def create(self, validated_data):
            return Comment.objects.create(**validated_data)
    
  2. The ModelViewset create() method. Documentation

    class AccountViewSet(viewsets.ModelViewSet):
    
        queryset = Account.objects.all()
        serializer_class = AccountSerializer
        permission_classes = [IsAccountAdminOrReadOnly]
    
  3. The ModelViewset perform_create() method. Documentation

    class SnippetViewSet(viewsets.ModelViewSet):
    
        def perform_create(self, serializer):
            serializer.save(owner=self.request.user)
    

These three approaches are important depending on your application environment.

But WHEN do we need to use each create() / perform_create() function??. On the other hand I found some account that two create methods were called for a single post request the modelviewset’s create() and serializer’s create().

Hopefully anyone would share some of their knowledge to explain and this will surely be very helpful in my development process.


回答 0

  1. 你会使用create(self, validated_data)节约型和“刺”的价值观为就像每个模型前场添加任何额外的细节到对象**validated_data一样。理想情况下,您只想在一个位置执行这种“探测”形式,因此create您的方法CommentSerializer是最佳的选择。最重要的是,您可能还想调用外部api,以在将帐户保存到自己的数据库之前在其旁边创建用户帐户。您应该将此create功能与结合使用ModelViewSet。永远想一想-“薄视图,厚串行器”。

例:

def create(self, validated_data):
    email = validated_data.get("email", None)
    validated.pop("email") 
    # Now you have a clean valid email string 
    # You might want to call an external API or modify another table
    # (eg. keep track of number of accounts registered.) or even
    # make changes to the email format.

    # Once you are done, create the instance with the validated data
    return models.YourModel.objects.create(email=email, **validated_data)
  1. 中的create(self, request, *args, **kwargs)函数在的父类中ModelViewSet定义。的主要功能如下:CreateModelMixinModelViewSetCreateModelMixin

    from rest_framework import status
    from rest_framework.response import Response
    
    
    def create(self, request, *args, **kwargs):
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        self.perform_create(serializer)
        headers = self.get_success_headers(serializer.data)
        return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
    
    def perform_create(self, serializer):
        serializer.save()
    

如您所见,以上create函数负责在序列化程序上调用验证并产生正确的响应。这样做的好处是,您现在可以隔离应用程序逻辑,而不必担心平凡和重复的验证调用以及处理响应输出:)。与create(self, validated_data)序列化器(您的特定应用程序逻辑所在的位置)中的结合使用时,这可以很好地工作。

  1. 现在您可能会问,为什么我们perform_create(self, serializer)只有一行代码才有一个单独的函数!好吧,这背后的主要原因是在调用save函数时允许自定义。您可能想要在调用之前提供额外的数据save (例如serializer.save(owner=self.request.user),如果我们没有perform_create(self, serializer),那么您将不得不重写,create(self, request, *args, **kwargs)而这违背了让mixin进行繁重而乏味的工作的目的。

希望这可以帮助!

  1. You would use create(self, validated_data) to add any extra details into the object before saving AND “prod” values into each model field just like **validated_data does. Ideally speaking, you want to do this form of “prodding” only in ONE location so the create method in your CommentSerializer is the best place. On top of this, you might want to also call external apis to create user accounts on their side just before saving your accounts into your own database. You should use this create function in conjunction withModelViewSet. Always think – “Thin views, Thick serializers”.

Example:

def create(self, validated_data):
    email = validated_data.get("email", None)
    validated.pop("email") 
    # Now you have a clean valid email string 
    # You might want to call an external API or modify another table
    # (eg. keep track of number of accounts registered.) or even
    # make changes to the email format.

    # Once you are done, create the instance with the validated data
    return models.YourModel.objects.create(email=email, **validated_data)
  1. The create(self, request, *args, **kwargs) function in the ModelViewSet is defined in the CreateModelMixin class which is the parent of ModelViewSet. CreateModelMixin‘s main functions are these:

    from rest_framework import status
    from rest_framework.response import Response
    
    
    def create(self, request, *args, **kwargs):
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        self.perform_create(serializer)
        headers = self.get_success_headers(serializer.data)
        return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
    
    def perform_create(self, serializer):
        serializer.save()
    

As you can see, the above create function takes care of calling validation on your serializer and producing the correct response. The beauty behind this, is that you can now isolate your application logic and NOT concern yourself about the mundane and repetitive validation calls and handling response output :). This works quite well in conjuction with the create(self, validated_data) found in the serializer (where your specific application logic might reside).

  1. Now you might ask, why do we have a separate perform_create(self, serializer) function with just one line of code!?!? Well, the main reason behind this is to allow customizeability when calling the save function. You might want to supply extra data before calling save (like serializer.save(owner=self.request.user) and if we didn’t have perform_create(self, serializer), you would have to override the create(self, request, *args, **kwargs) and that just defeats the purpose of having mixins doing the heavy and boring work.

Hope this helps!


在没有其他Django的情况下如何使用Django模板?

问题:在没有其他Django的情况下如何使用Django模板?

我想在我的(Python)代码中使用Django模板引擎,但是我没有构建基于Django的网站。如何在没有settings.py文件(和其他文件)且没有设置DJANGO_SETTINGS_MODULE环境变量的情况下使用它?

如果我运行以下代码:

>>> import django.template
>>> from django.template import Template, Context
>>> t = Template('My name is {{ my_name }}.')

我得到:

ImportError: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined.

I want to use the Django template engine in my (Python) code, but I’m not building a Django-based web site. How do I use it without having a settings.py file (and others) and having to set the DJANGO_SETTINGS_MODULE environment variable?

If I run the following code:

>>> import django.template
>>> from django.template import Template, Context
>>> t = Template('My name is {{ my_name }}.')

I get:

ImportError: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined.

回答 0

解决方案很简单。它实际上有据可查,但不太容易找到。(我必须仔细研究-当我尝试了几种其他的Google搜索时并没有出现。)

以下代码有效:

>>> from django.template import Template, Context
>>> from django.conf import settings
>>> settings.configure()
>>> t = Template('My name is {{ my_name }}.')
>>> c = Context({'my_name': 'Daryl Spitzer'})
>>> t.render(c)
u'My name is Daryl Spitzer.'

有关您可能要定义的某些设置(作为要配置的关键字参数)的说明,请参阅Django文档(上文链接)。

The solution is simple. It’s actually well documented, but not too easy to find. (I had to dig around — it didn’t come up when I tried a few different Google searches.)

The following code works:

>>> from django.template import Template, Context
>>> from django.conf import settings
>>> settings.configure()
>>> t = Template('My name is {{ my_name }}.')
>>> c = Context({'my_name': 'Daryl Spitzer'})
>>> t.render(c)
u'My name is Daryl Spitzer.'

See the Django documentation (linked above) for a description of some of the settings you may want to define (as keyword arguments to configure).


回答 1

Jinja2 语法与Django几乎相同,只是差别很小,而且您获得了功能更强大的模板引擎,该引擎还将模板编译为字节码(FAST!)。

我使用它来进行模板化,包括在Django本身中,它非常好。如果缺少某些所需功能,您也可以轻松编写扩展名。

这是代码生成的一些演示:

>>> import jinja2
>>> print jinja2.Environment().compile('{% for row in data %}{{ row.name | upper }}{% endfor %}', raw=True) 
from __future__ import division
from jinja2.runtime import LoopContext, Context, TemplateReference, Macro, Markup, TemplateRuntimeError, missing, concat, escape, markup_join, unicode_join
name = None

def root(context, environment=environment):
    l_data = context.resolve('data')
    t_1 = environment.filters['upper']
    if 0: yield None
    for l_row in l_data:
        if 0: yield None
        yield unicode(t_1(environment.getattr(l_row, 'name')))

blocks = {}
debug_info = '1=9'

Jinja2 syntax is pretty much the same as Django’s with very few differences, and you get a much more powerfull template engine, which also compiles your template to bytecode (FAST!).

I use it for templating, including in Django itself, and it is very good. You can also easily write extensions if some feature you want is missing.

Here is some demonstration of the code generation:

>>> import jinja2
>>> print jinja2.Environment().compile('{% for row in data %}{{ row.name | upper }}{% endfor %}', raw=True) 
from __future__ import division
from jinja2.runtime import LoopContext, Context, TemplateReference, Macro, Markup, TemplateRuntimeError, missing, concat, escape, markup_join, unicode_join
name = None

def root(context, environment=environment):
    l_data = context.resolve('data')
    t_1 = environment.filters['upper']
    if 0: yield None
    for l_row in l_data:
        if 0: yield None
        yield unicode(t_1(environment.getattr(l_row, 'name')))

blocks = {}
debug_info = '1=9'

回答 2

您要使用Django模板的任何特殊原因?无论神社元史是,在我看来,优越。


如果您确实愿意,请参见中的Django文档settings.py。特别是“使用设置而不设置DJANGO_SETTINGS_MODULE”部分。使用这样的东西:

from django.conf import settings
settings.configure (FOO='bar') # Your settings go here

Any particular reason you want to use Django’s templates? Both Jinja and Genshi are, in my opinion, superior.


If you really want to, then see the Django documentation on settings.py. Especially the section “Using settings without setting DJANGO_SETTINGS_MODULE“. Use something like this:

from django.conf import settings
settings.configure (FOO='bar') # Your settings go here

回答 3

我还建议jinja2。关于vs. 有一篇不错的文章,其中提供了一些详细信息,说明您为什么偏爱后者。djangojinja2

I would also recommend jinja2. There is a nice article on django vs. jinja2 that gives some in-detail information on why you should prefere the later.


回答 4

根据Jinja文档,Python 3支持仍处于试验阶段。因此,如果您使用的是Python 3,而性能不是问题,则可以使用django的内置模板引擎。

Django 1.8引入了对多个模板引擎的支持,这需要更改模板的初始化方式。您必须显式配置settings.DEBUGdjango提供的默认模板引擎使用的模板。这是不使用django其余部分即可使用模板的代码。

from django.template import Template, Context
from django.template.engine import Engine

from django.conf import settings
settings.configure(DEBUG=False)

template_string = "Hello {{ name }}"
template = Template(template_string, engine=Engine())
context = Context({"name": "world"})
output = template.render(context) #"hello world"

According to the Jinja documentation, Python 3 support is still experimental. So if you are on Python 3 and performance is not an issue, you can use django’s built in template engine.

Django 1.8 introduced support for multiple template engines which requires a change to the way templates are initialized. You have to explicitly configure settings.DEBUG which is used by the default template engine provided by django. Here’s the code to use templates without using the rest of django.

from django.template import Template, Context
from django.template.engine import Engine

from django.conf import settings
settings.configure(DEBUG=False)

template_string = "Hello {{ name }}"
template = Template(template_string, engine=Engine())
context = Context({"name": "world"})
output = template.render(context) #"hello world"

回答 5

除了其他人写的东西之外,如果要在Django> 1.7上使用Django模板,则必须给您的settings.configure(…)调用TEMPLATES变量,然后像这样调用django.setup():

from django.conf import settings

settings.configure(TEMPLATES=[
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': ['.'], # if you want the templates from a file
        'APP_DIRS': False, # we have no apps
    },
])

import django
django.setup()

然后,您可以像通常一样从字符串加载模板:

from django import template   
t = template.Template('My name is {{ name }}.')   
c = template.Context({'name': 'Rob'})   
t.render(c)

而且,如果您在磁盘中的.configure中写入了DIRS变量,则:

from django.template.loader import get_template
t = get_template('a.html')
t.render({'name': 5})

Django错误:未配置DjangoTemplates后端

http://django.readthedocs.io/zh-CN/latest/releases/1.7.html#standalone-scripts

An addition to what other wrote, if you want to use Django Template on Django > 1.7, you must give your settings.configure(…) call the TEMPLATES variable and call django.setup() like this :

from django.conf import settings

settings.configure(TEMPLATES=[
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': ['.'], # if you want the templates from a file
        'APP_DIRS': False, # we have no apps
    },
])

import django
django.setup()

Then you can load your template like normally, from a string :

from django import template   
t = template.Template('My name is {{ name }}.')   
c = template.Context({'name': 'Rob'})   
t.render(c)

And if you wrote the DIRS variable in the .configure, from the disk :

from django.template.loader import get_template
t = get_template('a.html')
t.render({'name': 5})

Django Error: No DjangoTemplates backend is configured

http://django.readthedocs.io/en/latest/releases/1.7.html#standalone-scripts


回答 6

我也会说Jinja。它绝对比Django Templating Engine 更强大,并且是独立的

如果这是现有Django应用程序的外部插件,则可以创建一个自定义命令,并在项目环境中使用模板引擎。像这样;

manage.py generatereports --format=html

但是我认为仅仅使用Django模板引擎而不是Jinja是不值得的。

I would say Jinja as well. It is definitely more powerful than Django Templating Engine and it is stand alone.

If this was an external plug to an existing Django application, you could create a custom command and use the templating engine within your projects environment. Like this;

manage.py generatereports --format=html

But I don’t think it is worth just using the Django Templating Engine instead of Jinja.


回答 7

谢谢大家的帮助。这是另外一个。您需要使用自定义模板标签的情况。

假设您在read.py模块中有这个重要的模板标签

from django import template

register = template.Library()

@register.filter(name='bracewrap')
def bracewrap(value):
    return "{" + value + "}"

这是html模板文件“ temp.html”:

{{var|bracewrap}}

最后,这是一个将所有内容捆绑在一起的Python脚本

import django
from django.conf import settings
from django.template import Template, Context
import os

#load your tags
from django.template.loader import get_template
django.template.base.add_to_builtins("read")

# You need to configure Django a bit
settings.configure(
    TEMPLATE_DIRS=(os.path.dirname(os.path.realpath(__file__)), ),
)

#or it could be in python
#t = Template('My name is {{ my_name }}.')
c = Context({'var': 'stackoverflow.com rox'})

template = get_template("temp.html")
# Prepare context ....
print template.render(c)

输出将是

{stackoverflow.com rox}

Thanks for the help folks. Here is one more addition. The case where you need to use custom template tags.

Let’s say you have this important template tag in the module read.py

from django import template

register = template.Library()

@register.filter(name='bracewrap')
def bracewrap(value):
    return "{" + value + "}"

This is the html template file “temp.html”:

{{var|bracewrap}}

Finally, here is a Python script that will tie to all together

import django
from django.conf import settings
from django.template import Template, Context
import os

#load your tags
from django.template.loader import get_template
django.template.base.add_to_builtins("read")

# You need to configure Django a bit
settings.configure(
    TEMPLATE_DIRS=(os.path.dirname(os.path.realpath(__file__)), ),
)

#or it could be in python
#t = Template('My name is {{ my_name }}.')
c = Context({'var': 'stackoverflow.com rox'})

template = get_template("temp.html")
# Prepare context ....
print template.render(c)

The output would be

{stackoverflow.com rox}

回答 8


回答 9

别。改用StringTemplate-一旦知道它,就没有理由考虑其他模板引擎了。

Don’t. Use StringTemplate instead–there is no reason to consider any other template engine once you know about it.


回答 10

我回应以上陈述。Jinja 2是通用模板的一个很好的Django模板超集。我认为他们正在努力使Django模板与settings.py的耦合度降低一些,但是Jinja应该为您做的更好。

I echo the above statements. Jinja 2 is a pretty good superset of Django templates for general use. I think they’re working on making the Django templates a little less coupled to the settings.py, but Jinja should do well for you.


回答 11

运行manage.py外壳程序时:

>>> from django import template   
>>> t = template.Template('My name is {{ me }}.')   
>>> c = template.Context({'me': 'ShuJi'})   
>>> t.render(c)

While running the manage.py shell:

>>> from django import template   
>>> t = template.Template('My name is {{ me }}.')   
>>> c = template.Context({'me': 'ShuJi'})   
>>> t.render(c)

回答 12

Google AppEngine使用Django模板引擎,您是否看过它们的工作方式?您可以使用它。

Google AppEngine uses the Django templating engine, have you taken a look at how they do it? You could possibly just use that.


Django:从数据库中获取对象,如果没有匹配项,则为“无”

问题:Django:从数据库中获取对象,如果没有匹配项,则为“无”

是否有任何Django函数可以让我从数据库中获取对象,如果没有匹配项,则为None?

现在我正在使用类似:

foo = Foo.objects.filter(bar=baz)
foo = len(foo) > 0 and foo.get() or None

但这还不是很清楚,到处都是混乱的。

Is there any Django function which will let me get an object form the database, or None if nothing matches?

Right now I’m using something like:

foo = Foo.objects.filter(bar=baz)
foo = len(foo) > 0 and foo.get() or None

But that’s not very clear, and it’s messy to have everywhere.


回答 0

在Django 1.6中,您可以使用first()Queryset方法。它返回查询集匹配的第一个对象,如果没有匹配的对象,则返回None。

用法:

p = Article.objects.order_by('title', 'pub_date').first()

In Django 1.6 you can use the first() Queryset method. It returns the first object matched by the queryset, or None if there is no matching object.

Usage:

p = Article.objects.order_by('title', 'pub_date').first()

回答 1

有两种方法可以做到这一点;

try:
    foo = Foo.objects.get(bar=baz)
except model.DoesNotExist:
    foo = None

或者,您可以使用包装器:

def get_or_none(model, *args, **kwargs):
    try:
        return model.objects.get(*args, **kwargs)
    except model.DoesNotExist:
        return None

这样称呼它

foo = get_or_none(Foo, baz=bar)

There are two ways to do this;

try:
    foo = Foo.objects.get(bar=baz)
except model.DoesNotExist:
    foo = None

Or you can use a wrapper:

def get_or_none(model, *args, **kwargs):
    try:
        return model.objects.get(*args, **kwargs)
    except model.DoesNotExist:
        return None

Call it like this

foo = get_or_none(Foo, baz=bar)

回答 2

要将一些示例代码添加到sorki的答案中(我将其添加为评论,但这是我的第一篇文章,并且我没有足够的声誉来发表评论),我实现了一个get_or_none自定义管理器,如下所示:

from django.db import models

class GetOrNoneManager(models.Manager):
    """Adds get_or_none method to objects
    """
    def get_or_none(self, **kwargs):
        try:
            return self.get(**kwargs)
        except self.model.DoesNotExist:
            return None

class Person(models.Model):
    name = models.CharField(max_length=255)
    objects = GetOrNoneManager()

现在我可以这样做:

bob_or_none = Person.objects.get_or_none(name='Bob')

To add some sample code to sorki’s answer (I’d add this as a comment, but this is my first post, and I don’t have enough reputation to leave comments), I implemented a get_or_none custom manager like so:

from django.db import models

class GetOrNoneManager(models.Manager):
    """Adds get_or_none method to objects
    """
    def get_or_none(self, **kwargs):
        try:
            return self.get(**kwargs)
        except self.model.DoesNotExist:
            return None

class Person(models.Model):
    name = models.CharField(max_length=255)
    objects = GetOrNoneManager()

And now I can do this:

bob_or_none = Person.objects.get_or_none(name='Bob')

回答 3

您也可以尝试使用django烦人(它还有另一个有用的功能!)

用以下命令安装:

pip install django-annoying

from annoying.functions import get_object_or_None
get_object_or_None(Foo, bar=baz)

You can also try to use django annoying (it has another useful functions!)

install it with:

pip install django-annoying

from annoying.functions import get_object_or_None
get_object_or_None(Foo, bar=baz)

回答 4

给Foo 自定义经理。这很容易-只需将代码放入自定义管理器的函数中,在模型中设置自定义管理器,然后使用即可调用Foo.objects.your_new_func(...)

如果您需要通用函数(不仅要在自定义管理器中使用它,还可以在任何模型上使用它),请编写自己的函数并将其放在python路径上并导入,而不是再麻烦了。

Give Foo its custom manager. It’s pretty easy – just put your code into function in custom manager, set custom manager in your model and call it with Foo.objects.your_new_func(...).

If you need generic function (to use it on any model not just that with custom manager) write your own and place it somewhere on your python path and import, not messy any more.


回答 5

无论是通过管理器还是通过通用函数执行操作,您都可能希望在TRY语句中捕获“ MultipleObjectsReturned”,因为如果kwarg检索了多个对象,则get()函数将引发此问题。

基于通用功能:

def get_unique_or_none(model, *args, **kwargs):
    try:
        return model.objects.get(*args, **kwargs)
    except (model.DoesNotExist, model.MultipleObjectsReturned), err:
        return None

并在经理中:

class GetUniqueOrNoneManager(models.Manager):
    """Adds get_unique_or_none method to objects
    """
    def get_unique_or_none(self, *args, **kwargs):
        try:
            return self.get(*args, **kwargs)
        except (self.model.DoesNotExist, self.model.MultipleObjectsReturned), err:
            return None

Whether doing it via a manager or generic function, you may also want to catch ‘MultipleObjectsReturned’ in the TRY statement, as the get() function will raise this if your kwargs retrieve more than one object.

Building on the generic function:

def get_unique_or_none(model, *args, **kwargs):
    try:
        return model.objects.get(*args, **kwargs)
    except (model.DoesNotExist, model.MultipleObjectsReturned), err:
        return None

and in the manager:

class GetUniqueOrNoneManager(models.Manager):
    """Adds get_unique_or_none method to objects
    """
    def get_unique_or_none(self, *args, **kwargs):
        try:
            return self.get(*args, **kwargs)
        except (self.model.DoesNotExist, self.model.MultipleObjectsReturned), err:
            return None

回答 6

这是helper函数的一种变体QuerySet,如果您希望从除模型all对象的查询集以外的查询集中获取唯一的对象(如果存在)(例如,从属于父实例):

def get_unique_or_none(model, queryset=None, *args, **kwargs):
    """
        Performs the query on the specified `queryset`
        (defaulting to the `all` queryset of the `model`'s default manager)
        and returns the unique object matching the given
        keyword arguments.  Returns `None` if no match is found.
        Throws a `model.MultipleObjectsReturned` exception
        if more than one match is found.
    """
    if queryset is None:
        queryset = model.objects.all()
    try:
        return queryset.get(*args, **kwargs)
    except model.DoesNotExist:
        return None

可以通过两种方式使用它,例如:

  1. obj = get_unique_or_none(Model, *args, **kwargs) 如前所述
  2. obj = get_unique_or_none(Model, parent.children, *args, **kwargs)

Here’s a variation on the helper function that allows you to optionally pass in a QuerySet instance, in case you want to get the unique object (if present) from a queryset other than the model’s all objects queryset (e.g. from a subset of child items belonging to a parent instance):

def get_unique_or_none(model, queryset=None, *args, **kwargs):
    """
        Performs the query on the specified `queryset`
        (defaulting to the `all` queryset of the `model`'s default manager)
        and returns the unique object matching the given
        keyword arguments.  Returns `None` if no match is found.
        Throws a `model.MultipleObjectsReturned` exception
        if more than one match is found.
    """
    if queryset is None:
        queryset = model.objects.all()
    try:
        return queryset.get(*args, **kwargs)
    except model.DoesNotExist:
        return None

This can be used in two ways, e.g.:

  1. obj = get_unique_or_none(Model, *args, **kwargs) as previosuly discussed
  2. obj = get_unique_or_none(Model, parent.children, *args, **kwargs)

回答 7

我认为在大多数情况下,您可以使用:

foo, created = Foo.objects.get_or_create(bar=baz)

仅在不关键要在Foo表中添加新条目的情况下(其他列将具有None / default值)

I think that in most cases you can just use:

foo, created = Foo.objects.get_or_create(bar=baz)

Only if it is not critical that a new entry will be added in Foo table ( other columns will have the None/default values )


AttributeError:“模块”对象没有属性“测试”

问题:AttributeError:“模块”对象没有属性“测试”

我正在运行以下命令:

python manage.py test project.apps.app1.tests

并导致此错误:

AttributeError:“模块”对象没有属性“测试”

下面是我的目录结构。我还已将app1添加到已安装的应用程序配置中。

Traceback (most recent call last):
    File "manage.py", line 10, in <module> execute_from_command_line(sys.argv)
    File "/home/username/local/dev/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
    File "/home/username/local/dev/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 377, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
    File "/home/username/local/dev/local/lib/python2.7/site-packages/django/core/management/commands/test.py", line 50, in run_from_argv
    super(Command, self).run_from_argv(argv)
    File "/home/username/local/dev/local/lib/python2.7/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
    File "/home/username/local/dev/local/lib/python2.7/site-packages/django/core/management/commands/test.py", line 71, in execute
    super(Command, self).execute(*args, **options)
    File "/home/username/local/dev/local/lib/python2.7/site-packages/django/core/management/base.py", line 338, in execute
    output = self.handle(*args, **options)
    File "/home/username/local/dev/local/lib/python2.7/site-packages/django/core/management/commands/test.py", line 88, in handle
    failures = test_runner.run_tests(test_labels)
    File "/home/username/local/dev/local/lib/python2.7/site-packages/django/test/runner.py", line 146, in run_tests
    suite = self.build_suite(test_labels, extra_tests)
    File "/home/username/local/dev/local/lib/python2.7/site-packages/django/test/runner.py", line 66, in build_suite
    tests = self.test_loader.loadTestsFromName(label)
    File "/usr/lib/python2.7/unittest/loader.py", line 100, in loadTestsFromName
    parent, obj = obj, getattr(obj, part)
    AttributeError: 'module' object has no attribute 'tests'

目录结构:

在此处输入图片说明

I’m running this command:

python manage.py test project.apps.app1.tests

and it causes this error:

AttributeError: ‘module’ object has no attribute ‘tests’

Below is my directory structure. I’ve also added app1 to my installed apps config.

Traceback (most recent call last):
    File "manage.py", line 10, in <module> execute_from_command_line(sys.argv)
    File "/home/username/local/dev/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
    File "/home/username/local/dev/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 377, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
    File "/home/username/local/dev/local/lib/python2.7/site-packages/django/core/management/commands/test.py", line 50, in run_from_argv
    super(Command, self).run_from_argv(argv)
    File "/home/username/local/dev/local/lib/python2.7/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
    File "/home/username/local/dev/local/lib/python2.7/site-packages/django/core/management/commands/test.py", line 71, in execute
    super(Command, self).execute(*args, **options)
    File "/home/username/local/dev/local/lib/python2.7/site-packages/django/core/management/base.py", line 338, in execute
    output = self.handle(*args, **options)
    File "/home/username/local/dev/local/lib/python2.7/site-packages/django/core/management/commands/test.py", line 88, in handle
    failures = test_runner.run_tests(test_labels)
    File "/home/username/local/dev/local/lib/python2.7/site-packages/django/test/runner.py", line 146, in run_tests
    suite = self.build_suite(test_labels, extra_tests)
    File "/home/username/local/dev/local/lib/python2.7/site-packages/django/test/runner.py", line 66, in build_suite
    tests = self.test_loader.loadTestsFromName(label)
    File "/usr/lib/python2.7/unittest/loader.py", line 100, in loadTestsFromName
    parent, obj = obj, getattr(obj, part)
    AttributeError: 'module' object has no attribute 'tests'

Directory structure:

enter image description here


回答 0

我终于想出了解决另一个问题的方法。问题是我的测试找不到导入。

如果您的测试无法导入,您似乎会收到上述错误。这是有道理的,因为测试套件无法导入损坏的测试。至少我认为这是正在发生的事情,因为我在测试文件中修复了导入,并确定导入已开始起作用。

要验证您的测试用例,只需尝试在python控制台中导入测试用例文件即可。

例:

from project.apps.app1.tests import *

I finally figured it out working on another problem. The problem was that my test couldn’t find an import.

It looks like you get the above error if your test fails to import. This makes sense because the test suite can’t import a broken test. At least I think this is what is going on because I fixed the import within my test file and sure enough it started working.

To validate your test case just try import the test case file in python console.

Example:

from project.apps.app1.tests import *

回答 1

用:

./manage.py shell

其次是

import myapp.tests

查找导入错误的性质。

Use:

./manage.py shell

followed by

import myapp.tests

to find the nature of the import error.


回答 2

就我而言,我需要在文件夹中创建一个空的__init__.pyapp/tests

For my case, I need to create an empty __init__.py in my app/tests folder


回答 3

上面的Steve Bradshaw的示例可解决导入错误(感谢Steve)。

其他类型的错误(例如ValueError)也可能导致

AttributeError: 'module' object has no attribute 'tests'

看看这些错误是什么

./manage.py shell
from myapp.tests import SomeTestCase
t = SomeTestCase()

Steve Bradshaw’s example above works for import errors (thanks Steve).

Other type of errors (e.g. ValueError) may also cause

AttributeError: 'module' object has no attribute 'tests'

to see what these errors are

./manage.py shell
from myapp.tests import SomeTestCase
t = SomeTestCase()

回答 4

我和克里斯有同样的错误。我删除了一个旧模型,然后运行tests.py,但是另一个文件(views.py)仍在尝试导入已删除的模型。

当我取出现在已经过时的import语句时,问题解决了。

I had the same error as Chris. I had deleted an old model, then run tests.py, but another file (views.py) was still trying to import the deleted model.

When I took out the now-obsolete import statement, problem solved.


回答 5

确保脚本中使用的所有模块均未损坏。我的意思是在您的导入语句中检查拼写。

# invalid import
from app.model.notification import Notification
# valid import
from app.models.notification import Notification

您可以通过在djano的交互式控制台中执行import语句来测试您的模块。

$root@13faefes8: python manage.py shell
Type "help", "copyright", "credits" or "license" for more information (InteractiveConsole)
>>> from app.model.notification import Notification
Traceback (most recent call last): 
   File "<console>", line 1, in <module>
ImportError: No module named model.notification

Make sure that all modules that you are using in your script are not broken. By this I mean check spelling in your import statements.

# invalid import
from app.model.notification import Notification
# valid import
from app.models.notification import Notification

You can test yours modules by executing imports statements in djano’s interactive console.

$root@13faefes8: python manage.py shell
Type "help", "copyright", "credits" or "license" for more information (InteractiveConsole)
>>> from app.model.notification import Notification
Traceback (most recent call last): 
   File "<console>", line 1, in <module>
ImportError: No module named model.notification

回答 6

我通过修复循环导入引用解决了错误“ AttributeError:模块’utils’没有属性’name_of_my_function’”。我的文件manage.py和utils.py每个都有一个指向彼此的import语句。

I resolved the error “AttributeError: module ‘utils’ has no attribute ‘name_of_my_function’ ” by fixing a circular import reference. My files manage.py and utils.py each had an import statement pointing at each other.


回答 7

根据django文档,当运行测试时,测试实用程序的默认行为是在名称以test开头的任何文件中查找所有测试用例(即unittest.TestCase的子类),自动从中构建测试套件。这些测试用例,然后运行该套件。

所以试试这个: python manage.py test tests.py

According to django document When you run your tests, the default behavior of the test utility is to find all the test cases (that is, subclasses of unittest.TestCase) in any file whose name begins with test, automatically build a test suite out of those test cases, and run that suite.

so try this : python manage.py test tests.py


回答 8

遇到相同的错误,但检查了所有原因列表,没有解决我的问题。

最后弄清楚,原因是导入但尚未使用的一种方法的名称不正确。尽管这是一个愚蠢的错误,但它确实发生了。

Got the same error, but checked all the reasons list here, did not fix my problem.

Finally figure it out that, the reason is that the name of one method that imported but not used yet is not correct. Though it is a stupid error, it happens.


回答 9

我有同样的错误。原来是因为我将我的模块命名为common.py,但是已经有一些其他common.py模块。我要做的就是重命名我的模块。

I had the same error. It turned out to be because I named my module common.py, yet there already was some other common.py module. All I had to do was to rename my module.


回答 10

编写unittest.TestCase时遇到类似的错误。当我按原样重新键入相同的方法定义时,它似乎起作用了!

我在PyCharm上注意到的唯一变化是第二次弹出“覆盖”图标,因为setup(self)方法需要覆盖TestCase中定义的原始方法。

在此处输入图片说明

I had a similar error while writing a unittest.TestCase. When I re-typed the same method definition as-is, it seemed to work !

The only change I noticed on PyCharm was the ‘override’ icon pop-up the 2nd time, as the setup(self) method needs to override the original method defined in TestCase.

enter image description here


我的Django安装在哪里?

问题:我的Django安装在哪里?

我使用Django,但需要查找默认模板和应用程序。

我不知道它的安装位置。

我怎么能找到那个?

I use Django but I need to find the default templates and applications.

I don’t know where it’s installed.

How can I find that?


回答 0

在CLI中,您可以执行以下操作:

>>> import django
>>> django
<module 'django' from '/usr/local/lib/python2.6/dist-packages/django/__init__.pyc'>

in the CLI you can do this:

>>> import django
>>> django
<module 'django' from '/usr/local/lib/python2.6/dist-packages/django/__init__.pyc'>

回答 1

$ python
>>> import django
>>> django.__file__
'/usr/local/lib/python2.7/site-packages/django/__init__.pyc'
$ python
>>> import django
>>> django.__file__
'/usr/local/lib/python2.7/site-packages/django/__init__.pyc'

回答 2

当前的最佳答案至少在Linux上不起作用。

从Django 教程中

如果您很难找到Django源文件在系统上的位置,请运行以下命令:

python -c "
import sys
sys.path = sys.path[1:]
import django
print(django.__path__)"

The current top answer doesn’t work, at least on linux.

From the Django tutorial:

If you have difficulty finding where the Django source files are located on your system, run the following command:

python -c "
import sys
sys.path = sys.path[1:]
import django
print(django.__path__)"

回答 3

在Microsft-Windows操作系统上:在python安装中的Lib / site-packages文件夹中。

On Microsft-Windows OS: In the Lib/site-packages folder inside your python installation.


回答 4

我正在描述的这种方法可跨操作系统使用…

您可以在命令行上尝试- python -c "from distutils.sysconfig import get_python_lib; print get_python_lib()"

这将为您提供基本目录。在此处输入,/django/然后在其中找到所有默认模板,管理模板等。

希望这可以帮助…

This approach I am describing works across operating systems…

You try this on your command line – python -c "from distutils.sysconfig import get_python_lib; print get_python_lib()"

This gives you the base directory. From there, type /django/ and here you find all the default templates, admin templates, etc.

Hope this helps…


回答 5

正如对@olafure的答案https://stackoverflow.com/a/12974642/4515198的评论所正确指出的那样,sys.path不需要分配。

以下内容就足够了:

python -c "import django; print(django.__path__)"

这里的-c选项用于告诉Python说“节目被传递作为字符串”(来源:命令$ python --helpbash

As the comments on @olafure’s answer https://stackoverflow.com/a/12974642/4515198 rightly say, the sys.path assignment is not required.

The following will be enough:

python -c "import django; print(django.__path__)"

Here the -c option is used to tell python that a “program is being passed in as string” (source: command $ python --help on bash)


回答 6

import django
django.__file__

输出将被赋予django文件夹的位置

'C:\\Users\\saigopi\\AppData\\Local\\Programs\\Python\\Python36\\lib\\site-packages\\django\\__init__.py'
import django
django.__file__

output will be given location of the django folder

'C:\\Users\\saigopi\\AppData\\Local\\Programs\\Python\\Python36\\lib\\site-packages\\django\\__init__.py'

回答 7

值得一提的是,如果您使用的是虚拟环境,则所有软件包都将位于项目的“ lib”下的根venv文件夹中。

Worth mentioning that if you are using a virtual env all the packages will be in your project’s root venv folder under “lib” …


回答 8

在终端上尝试一下。

$ python -v
import django # directory /home/user/.virtualenvs/myenv/local/lib/python2.7/site-packages/django
# some other imports.

Try this on an terminal.

$ python -v
import django # directory /home/user/.virtualenvs/myenv/local/lib/python2.7/site-packages/django
# some other imports.

回答 9

如果您使用的是virtualenv,则它将是:
/ home / user / path,安装django / django_directory / lib / python2.7 / site-packages / Django-1.8.1-py2.7.egg / django / contrib / admin的位置/templates/admin/base_site.html
base-site.html是默认模板。

If you are using virtualenv then it will be:
/home/user/path where you installed django/django_directory/lib/python2.7/site-packages/Django-1.8.1-py2.7.egg/django/contrib/admin/templates/admin/base_site.html
base-site.html is the default template.


在Django中优雅地设置Python日志记录

问题:在Django中优雅地设置Python日志记录

我还没有找到对自己满意的Django设置Python日志记录的方法。我的要求很简单:

  • 不同事件的不同日志处理程序-也就是说,我希望能够登录到不同的文件
  • 轻松访问我模块中的记录器。该模块应该可以轻松找到其记录器。
  • 应该容易适用于命令行模块。系统的一部分是独立的命令行或守护进程。这些模块应易于使用日志记录。

我当前的设置是使用logging.conf文件,然后在我登录的每个模块中记录设置。感觉不对。

您是否有喜欢的日志记录设置?请详细说明:如何设置配置(logging.conf在代码中使用或设置),在何处/何时启动记录器,以及如何在模块中访问它们等等。

I have yet to find a way of setting up Python logging with Django that I’m happy with. My requirements are fairly simple:

  • Different log handlers for different events – that is, I want to be able to log to different files
  • Easy access to loggers in my modules. The module should be able to find its logger with little effort.
  • Should be easily applicable to command-line modules. Parts of the system are stand-alone command line or daemon processes. Logging should be easily usable with these modules.

My current setup is to use a logging.conf file and setup logging in each module I log from. It doesn’t feel right.

Do you have a logging setup that you like? Please detail it: how do you setup the configuration (do you use logging.conf or set it up in code), where/when do you initiate the loggers, and how do you get access to them in your modules, etc.


回答 0

到目前为止,我发现的最好方法是在settings.py中初始化日志记录设置-其他地方。您可以使用配置文件,也可以分步进行编程-这仅取决于您的要求。关键是我通常使用级别和有时记录日志的方式将我想要的处理程序添加到根记录器中。过滤器将我想要的事件获取到适当的文件,控制台,系统日志等。您当然可以将处理程序添加到任何其他记录器中同样,但根据我的经验,通常不需要这样做。

在每个模块中,我使用

logger = logging.getLogger(__name__)

并将其用于记录模块中的事件(并且,如果我想进一步区分),请使用记录器,该记录器是上面创建的记录器的子级。

如果我的应用程序可能会在未配置settings.py中的日志记录的站点中使用,请在以下位置定义NullHandler:

#someutils.py

class NullHandler(logging.Handler):
    def emit(self, record):
        pass

null_handler = NullHandler()

并确保将其实例添加到我的使用日志记录的应用程序的模块中创建的所有记录器中。(注意:NullHandler已经在Python 3.1的日志记录包中,并且将在Python 2.7中使用。)

logger = logging.getLogger(__name__)
logger.addHandler(someutils.null_handler)

这样做是为了确保您的模块在未配置settings.py中的日志记录的站点中正常运行,并且不会收到任何令人讨厌的“找不到记录器XYZ处理程序”的消息(这是有关潜在警告的警告)错误配置的日志记录)。

通过这种方式可以满足您规定的要求:

  • 您可以像当前一样为不同的事件设置不同的日志处理程序。
  • 轻松访问模块中的记录器-使用getLogger(__name__)
  • 容易适用于命令行模块-它们也可以导入settings.py

更新:请注意,从1.3版开始,Django现在合并了对logging的支持

The best way I’ve found so far is to initialize logging setup in settings.py – nowhere else. You can either use a configuration file or do it programmatically step-by-step – it just depends on your requirements. The key thing is that I usually add the handlers I want to the root logger, using levels and sometimes logging.Filters to get the events I want to the appropriate files, console, syslogs etc. You can of course add handlers to any other loggers too, but there isn’t commonly a need for this in my experience.

In each module, I define a logger using

logger = logging.getLogger(__name__)

and use that for logging events in the module (and, if I want to differentiate further) use a logger which is a child of the logger created above.

If my app is going to be potentially used in a site which doesn’t configure logging in settings.py, I define a NullHandler somewhere as follows:

#someutils.py

class NullHandler(logging.Handler):
    def emit(self, record):
        pass

null_handler = NullHandler()

and ensure that an instance of it is added to all loggers created in the modules in my apps which use logging. (Note: NullHandler is already in the logging package for Python 3.1, and will be in Python 2.7.) So:

logger = logging.getLogger(__name__)
logger.addHandler(someutils.null_handler)

This is done to ensure that your modules play nicely in a site which doesn’t configure logging in settings.py, and that you don’t get any annoying “No handlers could be found for logger X.Y.Z” messages (which are warnings about potentially misconfigured logging).

Doing it this way meets your stated requirements:

  • You can set up different log handlers for different events, as you currently do.
  • Easy access to loggers in your modules – use getLogger(__name__).
  • Easily applicable to command-line modules – they also import settings.py.

Update: Note that as of version 1.3, Django now incorporates support for logging.


回答 1

我知道这已经是一个解决的答案,但是按照django> = 1.3,有一个新的日志记录设置。

从旧到新并不是自动的,所以我想在这里写下来。

当然,请查看django文档以了解更多信息。

这是基本的配置文件,默认情况下是使用django-admin createproject v1.3创建的-里程可能会随着最新的django版本而变化:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'mail_admins': {
            'level': 'ERROR',
            'class': 'django.utils.log.AdminEmailHandler',
        }
    },
    'loggers': {
        'django.request': {
            'handlers': ['mail_admins'],
            'level': 'ERROR',
            'propagate': True,
        }
    }
}

该结构基于标准的Python日志dictConfig,该命令规定了以下块:

  • formatters -相应的值将是一个dict,其中每个键是一个格式化程序ID,每个值是一个描述如何配置相应的Formatter实例的dict。
  • filters -相应的值将是一个dict,其中每个键是一个过滤器ID,每个值是一个描述如何配置相应的Filter实例的dict。
  • handlers-相应的值将是一个dict,其中每个键是一个处理程序ID,每个值是一个描述如何配置相应的Handler实例的dict。每个处理程序具有以下键:

    • class(必填)。这是处理程序类的完全限定名称。
    • level(可选的)。处理程序的级别。
    • formatter(可选的)。此处理程序的格式化程序的ID。
    • filters(可选的)。此处理程序的过滤器ID的列表。

我通常至少这样做:

  • 添加一个.log文件
  • 配置我的应用程序以写入此日志

转换为:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'verbose': {
            'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s'
        },
        'simple': {
            'format': '%(levelname)s %(message)s'
        },
    },
    'filters': {
        'require_debug_false': {
            '()': 'django.utils.log.RequireDebugFalse'
        }
    },
    'handlers': {
        'null': {
            'level':'DEBUG',
            'class':'django.utils.log.NullHandler',
        },
        'console':{
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
            'formatter': 'simple'
        },
        # I always add this handler to facilitate separating loggings
        'log_file':{
            'level': 'DEBUG',
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': os.path.join(VAR_ROOT, 'logs/django.log'),
            'maxBytes': '16777216', # 16megabytes
            'formatter': 'verbose'
        },
        'mail_admins': {
            'level': 'ERROR',
            'filters': ['require_debug_false'],
            'class': 'django.utils.log.AdminEmailHandler',
            'include_html': True,
        }
    },
    'loggers': {
        'django.request': {
            'handlers': ['mail_admins'],
            'level': 'ERROR',
            'propagate': True,
        },
        'apps': { # I keep all my of apps under 'apps' folder, but you can also add them one by one, and this depends on how your virtualenv/paths are set
            'handlers': ['log_file'],
            'level': 'INFO',
            'propagate': True,
        },
    },
    # you can also shortcut 'loggers' and just configure logging for EVERYTHING at once
    'root': {
        'handlers': ['console', 'mail_admins'],
        'level': 'INFO'
    },
}

编辑

请参阅现在始终记录请求异常故障单#16288

我更新了上面的示例conf,以明确包含针对mail_admins的正确过滤器,以便默认情况下,当debug为True时,不发送电子邮件。

您应该添加一个过滤器:

'filters': {
    'require_debug_false': {
        '()': 'django.utils.log.RequireDebugFalse'
    }
},

并将其应用于mail_admins处理程序:

    'mail_admins': {
        'level': 'ERROR',
        'filters': ['require_debug_false'],
        'class': 'django.utils.log.AdminEmailHandler',
        'include_html': True,
    }

否则,django.core.handers.base.handle_uncaught_exception如果settings.DEBUG为True,则不会将错误传递给django.request记录器。

如果您在Django 1.5中不执行此操作,则会得到一个

DeprecationWarning:您在’mail_admins’日志记录处理程序上未定义过滤器:添加隐式的debug-false-only过滤器

但是在django 1.4和django 1.5中,一切仍然可以正常工作。

**结束编辑**

该conf受到django doc中示例conf的强烈启发,但添加了日志文件部分。

我经常还会执行以下操作:

LOG_LEVEL = 'DEBUG' if DEBUG else 'INFO'

...
    'level': LOG_LEVEL
...

然后在我的python代码中,我总是添加一个NullHandler,以防万一没有定义任何日志配置。这样可以避免未指定任何处理程序的警告。对于不一定只在Django(ref)中调用的库特别有用

import logging
# Get an instance of a logger
logger = logging.getLogger(__name__)
class NullHandler(logging.Handler): #exists in python 3.1
    def emit(self, record):
        pass
nullhandler = logger.addHandler(NullHandler())

# here you can also add some local logger should you want: to stdout with streamhandler, or to a local file...

[…]

logger.warning('etc.etc.')

希望这可以帮助!

I know this is a solved answer already, but as per django >= 1.3 there’s a new logging setting.

Moving from old to new is not automatic, so I thought i’ll write it down here.

And of course checkout the django doc for some more.

This is the basic conf, created by default with django-admin createproject v1.3 – mileage might change with latest django versions:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'mail_admins': {
            'level': 'ERROR',
            'class': 'django.utils.log.AdminEmailHandler',
        }
    },
    'loggers': {
        'django.request': {
            'handlers': ['mail_admins'],
            'level': 'ERROR',
            'propagate': True,
        }
    }
}

This structure is based upon the standard Python logging dictConfig, that dictates the following blocks:

  • formatters – the corresponding value will be a dict in which each key is a formatter id and each value is a dict describing how to configure the corresponding Formatter instance.
  • filters – the corresponding value will be a dict in which each key is a filter id and each value is a dict describing how to configure the corresponding Filter instance.
  • handlers – the corresponding value will be a dict in which each key is a handler id and each value is a dict describing how to configure the corresponding Handler instance. Each handler has the following keys:

    • class (mandatory). This is the fully qualified name of the handler class.
    • level (optional). The level of the handler.
    • formatter (optional). The id of the formatter for this handler.
    • filters (optional). A list of ids of the filters for this handler.

I usually do at least this:

  • add a .log file
  • configure my apps to write to this log

Which translates into:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'verbose': {
            'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s'
        },
        'simple': {
            'format': '%(levelname)s %(message)s'
        },
    },
    'filters': {
        'require_debug_false': {
            '()': 'django.utils.log.RequireDebugFalse'
        }
    },
    'handlers': {
        'null': {
            'level':'DEBUG',
            'class':'django.utils.log.NullHandler',
        },
        'console':{
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
            'formatter': 'simple'
        },
        # I always add this handler to facilitate separating loggings
        'log_file':{
            'level': 'DEBUG',
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': os.path.join(VAR_ROOT, 'logs/django.log'),
            'maxBytes': '16777216', # 16megabytes
            'formatter': 'verbose'
        },
        'mail_admins': {
            'level': 'ERROR',
            'filters': ['require_debug_false'],
            'class': 'django.utils.log.AdminEmailHandler',
            'include_html': True,
        }
    },
    'loggers': {
        'django.request': {
            'handlers': ['mail_admins'],
            'level': 'ERROR',
            'propagate': True,
        },
        'apps': { # I keep all my of apps under 'apps' folder, but you can also add them one by one, and this depends on how your virtualenv/paths are set
            'handlers': ['log_file'],
            'level': 'INFO',
            'propagate': True,
        },
    },
    # you can also shortcut 'loggers' and just configure logging for EVERYTHING at once
    'root': {
        'handlers': ['console', 'mail_admins'],
        'level': 'INFO'
    },
}

edit

See request exceptions are now always logged and Ticket #16288:

I updated the above sample conf to explicitly include the correct filter for mail_admins so that, by default, emails are not sent when debug is True.

You should add a filter:

'filters': {
    'require_debug_false': {
        '()': 'django.utils.log.RequireDebugFalse'
    }
},

and apply it to the mail_admins handler:

    'mail_admins': {
        'level': 'ERROR',
        'filters': ['require_debug_false'],
        'class': 'django.utils.log.AdminEmailHandler',
        'include_html': True,
    }

Otherwise the django.core.handers.base.handle_uncaught_exception doesn’t pass errors to the ‘django.request’ logger if settings.DEBUG is True.

If you don’t do this in Django 1.5 you’ll get a

DeprecationWarning: You have no filters defined on the ‘mail_admins’ logging handler: adding implicit debug-false-only filter

but things will still work correctly BOTH in django 1.4 and django 1.5.

** end edit **

That conf is strongly inspired by the sample conf in the django doc, but adding the log file part.

I often also do the following:

LOG_LEVEL = 'DEBUG' if DEBUG else 'INFO'

...
    'level': LOG_LEVEL
...

Then in my python code I always add a NullHandler in case no logging conf is defined whatsoever. This avoid warnings for no Handler specified. Especially useful for libs that are not necessarily called only in Django (ref)

import logging
# Get an instance of a logger
logger = logging.getLogger(__name__)
class NullHandler(logging.Handler): #exists in python 3.1
    def emit(self, record):
        pass
nullhandler = logger.addHandler(NullHandler())

# here you can also add some local logger should you want: to stdout with streamhandler, or to a local file...

[…]

logger.warning('etc.etc.')

Hope this helps!


回答 2

我们urls.py使用logging.ini文件初始化顶级日志记录。

的位置在logging.ini中提供settings.py,仅此而已。

然后每个模块都执行

logger = logging.getLogger(__name__)

为了区分测试,开发和生产实例,我们有不同的logging.ini文件。在大多数情况下,我们有一个“控制台日志”,该日志仅发送到带有错误的stderr。我们有一个“应用程序日志”,它使用常规的滚动日志文件进入日志目录。

We initialize logging in the top-level urls.py by using a logging.ini file.

The location of the logging.ini is provided in settings.py, but that’s all.

Each module then does

logger = logging.getLogger(__name__)

To distinguish testing, development and production instances, we have different logging.ini files. For the most part, we have a “console log” that goes to stderr with Errors only. We have an “application log” that uses a regular rolling log file that goes to a logs directory.


回答 3

我目前正在使用自己创建的日志系统。它使用CSV格式进行记录。

django-csvlog

该项目仍然没有完整的文档,但是我正在研究它。

I am currently using a logging system, which I created myself. It uses CSV format for logging.

django-csvlog

This project still doesn’t have full documentation, but I am working on it.


列表中每个*项目的Django过滤器查询集__in

问题:列表中每个*项目的Django过滤器查询集__in

假设我有以下型号

class Photo(models.Model):
    tags = models.ManyToManyField(Tag)

class Tag(models.Model):
    name = models.CharField(max_length=50)

在一个视图中,我有一个带有活动过滤器的列表,称为category。我想过滤所有具有类别标签的照片对象。

我试过了:

Photo.objects.filter(tags__name__in=categories)

但这匹配类别中的任何项目,而不是所有项目。

因此,如果类别为[‘holiday’,’summer’],则我希望Photo带有假日和夏季标签。

能做到吗?

Let’s say I have the following models

class Photo(models.Model):
    tags = models.ManyToManyField(Tag)

class Tag(models.Model):
    name = models.CharField(max_length=50)

In a view I have a list with active filters called categories. I want to filter Photo objects which have all tags present in categories.

I tried:

Photo.objects.filter(tags__name__in=categories)

But this matches any item in categories, not all items.

So if categories would be [‘holiday’, ‘summer’] I want Photo’s with both a holiday and summer tag.

Can this be achieved?


回答 0

摘要:

正如jpic和sgallen在评论中所建议的那样,可以.filter()为每个类别添加一个选项。每filter增加一个,就会添加更多的联接,这对于少量的类别来说应该不是问题。

聚合 方法。对于大量类别,此查询将更短,甚至更快。

您还可以选择使用自定义查询


一些例子

测试设置:

class Photo(models.Model):
    tags = models.ManyToManyField('Tag')

class Tag(models.Model):
    name = models.CharField(max_length=50)

    def __unicode__(self):
        return self.name

In [2]: t1 = Tag.objects.create(name='holiday')
In [3]: t2 = Tag.objects.create(name='summer')
In [4]: p = Photo.objects.create()
In [5]: p.tags.add(t1)
In [6]: p.tags.add(t2)
In [7]: p.tags.all()
Out[7]: [<Tag: holiday>, <Tag: summer>]

使用链接过滤器方法:

In [8]: Photo.objects.filter(tags=t1).filter(tags=t2)
Out[8]: [<Photo: Photo object>]

结果查询:

In [17]: print Photo.objects.filter(tags=t1).filter(tags=t2).query
SELECT "test_photo"."id"
FROM "test_photo"
INNER JOIN "test_photo_tags" ON ("test_photo"."id" = "test_photo_tags"."photo_id")
INNER JOIN "test_photo_tags" T4 ON ("test_photo"."id" = T4."photo_id")
WHERE ("test_photo_tags"."tag_id" = 3  AND T4."tag_id" = 4 )

请注意,每个都为查询filter添加了更多内容JOINS

使用注释 方法

In [29]: from django.db.models import Count
In [30]: Photo.objects.filter(tags__in=[t1, t2]).annotate(num_tags=Count('tags')).filter(num_tags=2)
Out[30]: [<Photo: Photo object>]

结果查询:

In [32]: print Photo.objects.filter(tags__in=[t1, t2]).annotate(num_tags=Count('tags')).filter(num_tags=2).query
SELECT "test_photo"."id", COUNT("test_photo_tags"."tag_id") AS "num_tags"
FROM "test_photo"
LEFT OUTER JOIN "test_photo_tags" ON ("test_photo"."id" = "test_photo_tags"."photo_id")
WHERE ("test_photo_tags"."tag_id" IN (3, 4))
GROUP BY "test_photo"."id", "test_photo"."id"
HAVING COUNT("test_photo_tags"."tag_id") = 2

ANDed Q对象不起作用:

In [9]: from django.db.models import Q
In [10]: Photo.objects.filter(Q(tags__name='holiday') & Q(tags__name='summer'))
Out[10]: []
In [11]: from operator import and_
In [12]: Photo.objects.filter(reduce(and_, [Q(tags__name='holiday'), Q(tags__name='summer')]))
Out[12]: []

结果查询:

In [25]: print Photo.objects.filter(Q(tags__name='holiday') & Q(tags__name='summer')).query
SELECT "test_photo"."id"
FROM "test_photo"
INNER JOIN "test_photo_tags" ON ("test_photo"."id" = "test_photo_tags"."photo_id")
INNER JOIN "test_tag" ON ("test_photo_tags"."tag_id" = "test_tag"."id")
WHERE ("test_tag"."name" = holiday  AND "test_tag"."name" = summer )

Summary:

One option is, as suggested by jpic and sgallen in the comments, to add .filter() for each category. Each additional filter adds more joins, which should not be a problem for small set of categories.

There is the aggregation approach. This query would be shorter and perhaps quicker for a large set of categories.

You also have the option of using custom queries.


Some examples

Test setup:

class Photo(models.Model):
    tags = models.ManyToManyField('Tag')

class Tag(models.Model):
    name = models.CharField(max_length=50)

    def __unicode__(self):
        return self.name

In [2]: t1 = Tag.objects.create(name='holiday')
In [3]: t2 = Tag.objects.create(name='summer')
In [4]: p = Photo.objects.create()
In [5]: p.tags.add(t1)
In [6]: p.tags.add(t2)
In [7]: p.tags.all()
Out[7]: [<Tag: holiday>, <Tag: summer>]

Using chained filters approach:

In [8]: Photo.objects.filter(tags=t1).filter(tags=t2)
Out[8]: [<Photo: Photo object>]

Resulting query:

In [17]: print Photo.objects.filter(tags=t1).filter(tags=t2).query
SELECT "test_photo"."id"
FROM "test_photo"
INNER JOIN "test_photo_tags" ON ("test_photo"."id" = "test_photo_tags"."photo_id")
INNER JOIN "test_photo_tags" T4 ON ("test_photo"."id" = T4."photo_id")
WHERE ("test_photo_tags"."tag_id" = 3  AND T4."tag_id" = 4 )

Note that each filter adds more JOINS to the query.

Using annotation approach:

In [29]: from django.db.models import Count
In [30]: Photo.objects.filter(tags__in=[t1, t2]).annotate(num_tags=Count('tags')).filter(num_tags=2)
Out[30]: [<Photo: Photo object>]

Resulting query:

In [32]: print Photo.objects.filter(tags__in=[t1, t2]).annotate(num_tags=Count('tags')).filter(num_tags=2).query
SELECT "test_photo"."id", COUNT("test_photo_tags"."tag_id") AS "num_tags"
FROM "test_photo"
LEFT OUTER JOIN "test_photo_tags" ON ("test_photo"."id" = "test_photo_tags"."photo_id")
WHERE ("test_photo_tags"."tag_id" IN (3, 4))
GROUP BY "test_photo"."id", "test_photo"."id"
HAVING COUNT("test_photo_tags"."tag_id") = 2

ANDed Q objects would not work:

In [9]: from django.db.models import Q
In [10]: Photo.objects.filter(Q(tags__name='holiday') & Q(tags__name='summer'))
Out[10]: []
In [11]: from operator import and_
In [12]: Photo.objects.filter(reduce(and_, [Q(tags__name='holiday'), Q(tags__name='summer')]))
Out[12]: []

Resulting query:

In [25]: print Photo.objects.filter(Q(tags__name='holiday') & Q(tags__name='summer')).query
SELECT "test_photo"."id"
FROM "test_photo"
INNER JOIN "test_photo_tags" ON ("test_photo"."id" = "test_photo_tags"."photo_id")
INNER JOIN "test_tag" ON ("test_photo_tags"."tag_id" = "test_tag"."id")
WHERE ("test_tag"."name" = holiday  AND "test_tag"."name" = summer )

回答 1

尽管仅适用于PostgreSQL,另一种有效的方法是使用django.contrib.postgres.fields.ArrayField

docs复制的示例:

>>> Post.objects.create(name='First post', tags=['thoughts', 'django'])
>>> Post.objects.create(name='Second post', tags=['thoughts'])
>>> Post.objects.create(name='Third post', tags=['tutorial', 'django'])

>>> Post.objects.filter(tags__contains=['thoughts'])
<QuerySet [<Post: First post>, <Post: Second post>]>

>>> Post.objects.filter(tags__contains=['django'])
<QuerySet [<Post: First post>, <Post: Third post>]>

>>> Post.objects.filter(tags__contains=['django', 'thoughts'])
<QuerySet [<Post: First post>]>

ArrayField具有一些更强大的功能,例如重叠索引转换

Another approach that works, although PostgreSQL only, is using django.contrib.postgres.fields.ArrayField:

Example copied from docs:

>>> Post.objects.create(name='First post', tags=['thoughts', 'django'])
>>> Post.objects.create(name='Second post', tags=['thoughts'])
>>> Post.objects.create(name='Third post', tags=['tutorial', 'django'])

>>> Post.objects.filter(tags__contains=['thoughts'])
<QuerySet [<Post: First post>, <Post: Second post>]>

>>> Post.objects.filter(tags__contains=['django'])
<QuerySet [<Post: First post>, <Post: Third post>]>

>>> Post.objects.filter(tags__contains=['django', 'thoughts'])
<QuerySet [<Post: First post>]>

ArrayField has some more powerful features such as overlap and index transforms.


回答 2

这也可以通过使用Django ORM和一些Python魔术来动态查询生成来完成:)

from operator import and_
from django.db.models import Q

categories = ['holiday', 'summer']
res = Photo.filter(reduce(and_, [Q(tags__name=c) for c in categories]))

想法是为每个类别生成适当的Q对象,然后使用AND运算符将它们组合到一个QuerySet中。例如,对于您的示例,它等于

res = Photo.filter(Q(tags__name='holiday') & Q(tags__name='summer'))

This also can be done by dynamic query generation using Django ORM and some Python magic :)

from operator import and_
from django.db.models import Q

categories = ['holiday', 'summer']
res = Photo.filter(reduce(and_, [Q(tags__name=c) for c in categories]))

The idea is to generate appropriate Q objects for each category and then combine them using AND operator into one QuerySet. E.g. for your example it’d be equal to

res = Photo.filter(Q(tags__name='holiday') & Q(tags__name='summer'))

回答 3

我使用了一个小函数,它为给定的运算符和列名迭代列表上的过滤器:

def exclusive_in (cls,column,operator,value_list):         
    myfilter = column + '__' + operator
    query = cls.objects
    for value in value_list:
        query=query.filter(**{myfilter:value})
    return query  

这个函数可以这样调用:

exclusive_in(Photo,'tags__name','iexact',['holiday','summer'])

它也可以与任何类和列表中的更多标签一起使用;运算符可以是’iexact’,’in’,’contains’,’ne’,…等任何人。

I use a little function that iterates filters over a list for a given operator an a column name :

def exclusive_in (cls,column,operator,value_list):         
    myfilter = column + '__' + operator
    query = cls.objects
    for value in value_list:
        query=query.filter(**{myfilter:value})
    return query  

and this function can be called like that:

exclusive_in(Photo,'tags__name','iexact',['holiday','summer'])

it also work with any class and more tags in the list; operators can be anyone like ‘iexact’,’in’,’contains’,’ne’,…


回答 4

queryset = Photo.objects.filter(tags__name="vacaciones") | Photo.objects.filter(tags__name="verano")
queryset = Photo.objects.filter(tags__name="vacaciones") | Photo.objects.filter(tags__name="verano")

回答 5

如果我们想动态地执行此操作,请遵循以下示例:

tag_ids = [t1.id, t2.id]
qs = Photo.objects.all()

for tag_id in tag_ids:
    qs = qs.filter(tag__id=tag_id)    

print qs

If we want to do it dynamically, followed the example:

tag_ids = [t1.id, t2.id]
qs = Photo.objects.all()

for tag_id in tag_ids:
    qs = qs.filter(tag__id=tag_id)    

print qs