使用virtualenv,我使用默认版本的Python(2.7)运行我的项目.在一个项目中,我需要使用Python 3.4.
我brew install python3以前在Mac上安装它.现在,我如何创建一个使用新版本的virtualenv?
例如sudo virtualenv envPython3
如果我尝试:
virtualenv -p python3 test
Run Code Online (Sandbox Code Playgroud)
我明白了:
Running virtualenv with interpreter /usr/local/bin/python3
Using base prefix '/usr/local/Cellar/python3/3.4.0_1/Frameworks/Python.framework/Versions/3.4'
New python executable in test/bin/python3.4
Also creating executable in test/bin/python
Failed to import the site module
Traceback (most recent call last):
File "/Users/user/Documents/workspace/test/test/bin/../lib/python3.4/site.py", line 67, in <module>
import os
File "/Users/user/Documents/workspace/test/test/bin/../lib/python3.4/os.py", line 634, in <module>
from _collections_abc import MutableMapping
ImportError: No module named '_collections_abc'
ERROR: The executable test/bin/python3.4 is not functioning
ERROR: It …Run Code Online (Sandbox Code Playgroud) 阅读Django文档:
get_user_model()
您应该使用django.contrib.auth.get_user_model()来引用用户模型,而不是直接引用User.此方法将返回当前活动的用户模型 - 如果指定了自定义用户模型,则返回自定义用户模型,否则返回User.
为User模型定义外键或多对多关系时,应使用AUTH_USER_MODEL设置指定自定义模型.
我对上面的文字感到困惑.我应该这样做:
author = models.ForeignKey(settings.AUTH_USER_MODEL)
Run Code Online (Sandbox Code Playgroud)
或这个...
author = models.ForeignKey(get_user_model())
Run Code Online (Sandbox Code Playgroud)
两者似乎都有效.
我已经阅读了Django Rest Framework指南并完成了所有教程.一切似乎都有意义,并且应该如何运作.我按照描述进行基本和会话认证. http://django-rest-framework.org/api-guide
但是,我正在努力使用文档的令牌认证部分,它有点缺乏或没有深入到教程的深度.
http://django-rest-framework.org/api-guide/authentication/#tokenauthentication
它说我需要为用户创建令牌,但是在models.py中说明了哪里?
我的问题是:
有人可以为第一个计时器解释文档的令牌认证部分吗?
我有一个CPU密集型Celery任务.我想在许多EC2实例中使用所有处理能力(核心)来更快地完成这项工作(我认为芹菜并行分布式多任务处理任务).
术语,线程,多处理,分布式计算,分布式并行处理都是我试图更好理解的术语.
示例任务:
@app.task
for item in list_of_millions_of_ids:
id = item # do some long complicated equation here very CPU heavy!!!!!!!
database.objects(newid=id).save()
Run Code Online (Sandbox Code Playgroud)
使用上面的代码(如果可能的话,有一个例子)如何使用Celery分配这个任务,允许利用云中所有可用机器的所有计算CPU功率来分离这一任务?
在Dockerfile中我有一个安装层requirements.txt:
FROM python:2.7
RUN pip install -r requirements.txt
Run Code Online (Sandbox Code Playgroud)
当我构建docker镜像时,无论对此文件所做的任何更改,它都会运行整个过程.
如何pip install -r requirements.txt在文件发生更改时确保仅运行Docker ?
Removing intermediate container f98c845d0f05
Step 3 : RUN pip install -r requirements.txt
---> Running in 8ceb63abaef6
Collecting https://github.com/tomchristie/django-rest-framework/archive/master.zip (from -r requirements.txt (line 30))
Downloading https://github.com/tomchristie/django-rest-framework/archive/master.zip
Collecting Django==1.8.7 (from -r requirements.txt (line 1))
Run Code Online (Sandbox Code Playgroud) 我有以下代码:
for (i in awards) {
if (awards[i] instanceof Array === false) {
console.log(awards[i]);
httpFactory.patch(awards[i], {"read": true}, false);
}
}
Run Code Online (Sandbox Code Playgroud)
我的IDE显示与上述代码相关的此错误:
可能对意外(自定义/继承)成员进行迭代,可能缺少hasOwnProperty检查
检查JavaScript中未过滤的for-in循环的任何实例.使用此构造会导致处理继承或意外的属性.您需要使用hasOwnProperty()方法过滤自己的属性.验证适用于JavaScript,html或jsp文件.
你能更详细地解释一下这个陈述的含义吗?
has_related_object我的模型中有一个方法需要检查相关对象是否存在
class Business(base):
name = models.CharField(max_length=100, blank=True, null=True)
def has_related_object(self):
return (self.customers is not None) and (self.car is not None)
class Customer(base):
name = models.CharField(max_length=100, blank=True, null=True)
person = models.OneToOneField('Business', related_name="customer")
Run Code Online (Sandbox Code Playgroud)
但我得到错误:
Business.has_related_object()
RelatedObjectDoesNotExist:业务没有客户.
我希望使用经度和纬度来存储我的用户位置,目前这是来自谷歌地图,但我将使用GeoDango和一些点来计算点到点之间的距离.
然而,我的第一个困惑是我应该使用Django中的哪个字段来存储经度和纬度值?我得到的信息是冲突的.
官方文档使用FloatField
https://docs.djangoproject.com/en/dev/ref/contrib/gis/tutorial/#geographic-models
lon = models.FloatField()
lat = models.FloatField()
Run Code Online (Sandbox Code Playgroud)
几乎所有关于stackoverflow的答案都显示了 DecimalField
long = models.DecimalField(max_digits=8, decimal_places=3)
lat = models.DecimalField(max_digits=8, decimal_places=3)
Run Code Online (Sandbox Code Playgroud)
那么我应该使用什么?
使用Django REST API,我正在尝试验证我的请求.
这就是我要发送的内容:
Content-Type: application/json, Authentication: token="6d82549b48a8b079f618ee9c51a6dfb59c7e2196"
Run Code Online (Sandbox Code Playgroud)
这就是我得到的回报:
{"detail": "Authentication credentials were not provided."}
Run Code Online (Sandbox Code Playgroud)
有人能给我正确的标题吗?
谢谢
标题:
Accept: application/json
Content-Type: application/json
Authorization: Token 6d82549b48a8b079f618ee9c51a6dfb59c7e2196
Connection: keep-alive
Origin: chrome-extension: //rest-console-id
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.57 Safari/537.17
Run Code Online (Sandbox Code Playgroud)

Settings.py
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.authentication.TokenAuthentication',
'rest_framework.permissions.IsAdminUser',
),
'PAGINATE_BY': 10
}
Run Code Online (Sandbox Code Playgroud)
view.py
class ProfileList(generics.ListCreateAPIView):
"""
API endpoint that represents a list of users.
"""
permission_classes = (permissions.IsAuthenticated,)
model = Profile
serializer_class = ProfileSerializer
def …Run Code Online (Sandbox Code Playgroud) 我只是在玩学习类函数等,所以我决定创建一个简单的函数,应该给我什么税额.
到目前为止这是我的代码.
class VAT_calculator:
"""
A set of methods for VAT calculations.
"""
def __init__(self, amount=None):
self.amount = amount
self.VAT = decimal.Decimal('0.095')
def initialize(self):
self.amount = 0
def total_with_VAT(self):
"""
Returns amount with VAT added.
"""
if not self.amount:
msg = u"Cannot add VAT if no amount is passed!'"
raise ValidationError(msg)
return (self.amount * self.VAT).quantize(self.amount, rounding=decimal.ROUND_UP)
Run Code Online (Sandbox Code Playgroud)
我的问题是我收到以下错误:
不支持的操作数类型*:'float'和'Decimal'**
我不明白为什么这不起作用!
python ×8
django ×7
celery ×1
docker ×1
dockerfile ×1
geodjango ×1
javascript ×1
python-3.x ×1
virtualenv ×1