我正在尝试安装MySQL_python适配器的1.2.2版本,使用使用该--no-site-packages
选项创建的新virtualenv .PyPi中显示的当前版本是1.2.3.有没有办法安装旧版本?我发现一篇文章说这应该这样做:
pip install MySQL_python==1.2.2
Run Code Online (Sandbox Code Playgroud)
但是,安装后,它仍会在站点包中显示MySQL_python-1.2.3-py2.6.egg-info.这是这个包特有的问题,还是我做错了什么?
对于OSX 10.6用户来说,这是一个备受争议的问题,但我找不到有效的解决方案.这是我的设置:
Python 2.6.1 64bit Django 1.2.1 MySQL 5.1.47 osx10.6 64bit
我用--no-site-packages创建了一个virtualenvwrapper,然后安装了Django.当我激活virtualenv并运行python manage.py syncdb时,我收到此错误:
Traceback (most recent call last):
File "manage.py", line 11, in <module>
execute_manager(settings)
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/core/management/__init__.py", line 438, in execute_manager
utility.execute()
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/core/management/__init__.py", line 379, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/core/management/__init__.py", line 257, in fetch_command
klass = load_command_class(app_name, subcommand)
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/core/management/__init__.py", line 67, in load_command_class
module = import_module('%s.management.commands.%s' % (app_name, name))
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/utils/importlib.py", line 35, in import_module
__import__(name)
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/core/management/commands/syncdb.py", line 7, in <module>
from django.core.management.sql import custom_sql_for_model, emit_post_sync_signal …
Run Code Online (Sandbox Code Playgroud) 尝试导入包含联系信息的CSV:
Name,Address,City,State,ZIP
Jane Doe,123 Main St,Whereverville,CA,90210
John Doe,555 Broadway Ave,New York,NY,10010
Run Code Online (Sandbox Code Playgroud)
运行此似乎不会向数据库添加任何文档:
$ mongoimport -d mydb -c things --type csv --file locations.csv --headerline
Run Code Online (Sandbox Code Playgroud)
Trace说imported 1 objects
,但启动Mongo shell并运行db.things.find()
并不会显示任何新文档.
我错过了什么?
我的网站使用Django的用户身份验证用户模型和自定义UserProfile模型来存储一些额外的数据(生日等).有没有办法在Django管理员中创建一个视图,将来自User和UserProfile模型的字段编织在一起?
我怀疑这个代码片段甚至不是很接近,但它可能有助于说明我正在尝试做什么:
from django.contrib import admin
from django.contrib.auth.models import User
from userprofile.models import UserProfile
class UserProfileAdmin(admin.ModelAdmin):
list_display = ('name', 'gender', 'User.email') #user.email creates the error - tried some variations here, but no luck.
admin.site.register(UserProfile, UserProfileAdmin)
Run Code Online (Sandbox Code Playgroud)
错误信息:
NotperlyConfigured:UserProfileAdmin.list_display [2],'User.email'不是'UserProfileAdmin'的可调用或属性,或者在模型'UserProfile'中找到.
最终,我正在尝试创建一个管理视图,其中包含来自UserProfile的姓名和来自User的电子邮件.
这是这个问题的后续行动.如何在父级的内联中显示在子模型上定义的属性?为了说明,我有这个模型:
class UserProfile(models.Model):
user = models.ForeignKey(User, unique=True, primary_key=True, related_name='profile')
...
@property
def age(self):
if self.birthday is None:
return None
td = datetime.date.today() - self.birthday
return td.days / 365
Run Code Online (Sandbox Code Playgroud)
问题是:如何在用户的内联中显示"年龄"?这就是我所拥有的:
class UserProfileInline(admin.StackedInline):
model = UserProfile
fk_name = 'user'
max_num = 1
fieldsets = [
('Demographics', {'fields': ['birthday', 'age']}),
]
Run Code Online (Sandbox Code Playgroud)
我尝试过这样的一些事情,包括'age()',为Inline定义'get_age'getter等等.它们导致了这个错误的某些版本:
ImproperlyConfigured: 'UserProfileInline.fieldsets[1][1]['fields']' refers to field 'age' that is missing from the form.
Run Code Online (Sandbox Code Playgroud) 我无法使用我创建的任何超级用户登录Django admin.试图创建新的超级用户,更改密码等 - 没有任何这些进程的错误消息,但仍然无法登录.
我不确定它是否相关,但我也无法运行django-admin.py命令.这是我到目前为止所做的:
$ django-admin.py validate
Error: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined.
$ export DJANGO_SETTINGS_MODULE=mysite.settings
$ django-admin.py validate
Error: Could not import settings 'mysite.settings' (Is it on sys.path? Does it have syntax errors?): No module named mysite.settings
Run Code Online (Sandbox Code Playgroud)
sys.path显示['/ Users/joerobinson/Sites/django/mysite'...(路径上的其他内容)]
注册mysite模块还需要做些什么吗?
python manage.py runserver工作正常.
结束语
关于登录django admin的问题,我没有在我的AUTHENTICATION_BACKENDS中包含django.contrib.auth.backends.ModelBackend - 添加这个允许我登录admin.
我还在研究django-admin.py配置问题(似乎没有关系),并将在一个单独的问题中重新打开它.
我正在通过Django教程并在运行初始python manage.py syncdb时收到以下错误:
Traceback (most recent call last):
File "manage.py", line 11, in <module>
execute_manager(settings)
File "/Library/Python/2.6/site-packages/django/core/management/__init__.py", line 362 in execute_manager
utility.execute()
File "/Library/Python/2.6/site-packages/django/core/management/__init__.py", line 303, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Library/Python/2.6/site-packages/django/core/management/base.py", line 195, in run_from_argv
self.execute(*args, **options.__dict__)
File "/Library/Python/2.6/site-packages/django/core/management/base.py", line 222, in execute
output = self.handle(*args, **options)
File "/Library/Python/2.6/site-packages/django/core/management/base.py", line 351, in handle
return self.handle_noargs(**options)
File "/Library/Python/2.6/site-packages/django/core/management/commands/syncdb.py", line 49, in handle_noargs
cursor = connection.cursor()
File "/Library/Python/2.6/site-packages/django/db/backends/dummy/base.py", line 15, in complain
raise ImproperlyConfigured, "You haven't set the DATABASE_ENGINE setting yet."
django.core.exceptions.ImproperlyConfigured: …
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用MongoDB作为Celery的消息队列(在Django应用程序中).Celery(2.2.0rc2)的当前开发版本应该允许你这样做,但我似乎无法让任何工作人员去接受我正在创建的任务.
版本:芹菜v2.2.0rc3
mongodb 1.6.5
pymongo 1.9
django-celery 2.2.0rc2
在我的设置中,我有:
CELERY_RESULT_BACKEND = "mongodb"
CELERY_MONGODB_BACKEND_SETTINGS = {
# Shouldn't need these - defaults are correct.
"host": "localhost",
"port": 27017,
"database": "celery",
"taskmeta_collection": "messages",
}
BROKER_BACKEND = 'mongodb'
BROKER_HOST = "localhost"
BROKER_PORT = 27017
BROKER_USER = ""
BROKER_PASSWORD = ""
BROKER_VHOST = ""
import djcelery
djcelery.setup_loader()
Run Code Online (Sandbox Code Playgroud)
我已经创建了一个测试tasks.py文件,如下所示:
from celery.decorators import task
@task()
def add(x, y):
return x + y
Run Code Online (Sandbox Code Playgroud)
如果我在后台启动芹菜,它似乎正常启动.然后我打开一个python shell并运行以下命令:
>>> from myapp.tasks import add
>>> result = add.delay(5,5)
>>> result
<AsyncResult: …
Run Code Online (Sandbox Code Playgroud) 我最近升级到python2.7并注意到tab键在shell中无法正常运行.相反,它似乎搜索基础目录(标准的unix行为).
如果我改回到python2.6,它会正常工作.有没有办法将此功能添加回2.7?
例如:
if foo :(
tab here)print'bar'#desired behavior is tab键在这里添加缩进
解决方案:
这是Mac OSX上python2.7的已知问题.我使用以下解决方法来纠正它:
$ cat > $HOME/.pystartup
import readline
if 'libedit' in readline.__doc__:
readline.parse_and_bind("bind ^I ed-insert")
^D
$ export PYTHONSTARTUP=$HOME/.pystartup
Run Code Online (Sandbox Code Playgroud) 我已经在我的Django项目中为Google OAuth2 实现了python-social-auth库,并能够成功使用它登录用户。该库将access_token
收到的信息存储在Google OAuth2流的响应中。
我的问题是:使用google-api-python-client似乎依赖于创建和授权credentials
对象,然后使用它来构建API,service
如下所示:
...
# send user to Google consent URL, get auth_code in response
credentials = flow.step2_exchange(auth_code)
http_auth = credentials.authorize(httplib2.Http())
from apiclient.discovery import build
service = build('gmail', 'v1', http=http_auth)
# use service for API calls...
Run Code Online (Sandbox Code Playgroud)
由于我是从access_token
python-social-auth提供的开始,因此如何service
为未来的API调用创建和授权API客户端?
编辑:澄清一下,上面的代码来自Google提供的示例。
python oauth2client google-api-python-client python-social-auth google-oauth2
django ×6
python ×4
django-admin ×3
mysql ×3
mongodb ×2
celery ×1
csv ×1
database ×1
import ×1
mongoimport ×1
mysql-python ×1
oauth2client ×1
pip ×1
pypi ×1
python-2.7 ×1
rabbitmq ×1
shell ×1
virtualenv ×1