我正在定义一组模型,它们相互引用.它们是文档应用程序的模型,如下所示
class Document(models.Model):
text = models.TextField()
class Chapter(models.Model):
doc = models.ForeignKey('Document')
chapter = models.IntegerField()
Run Code Online (Sandbox Code Playgroud)
我希望整个字段对于每个文档都是唯一的,但我不知道如何这样做.我知道每个字段都有一个唯一的参数,但它似乎对整个表都是唯一的,这不是我想要的.
我想知道如何让mongoengine和djangoREST框架相互合作.目前,我的模型是
from mongoengine import *
import datetime
class Blog(Document):
post_id = IntField(unique=True)
title = StringField(max_length=144, required=True)
date_created = DateTimeField(default=datetime.datetime.now)
body = StringField(required=True)
Run Code Online (Sandbox Code Playgroud)
我将序列化程序定义为
from rest_framework.views import APIView
from rest_framework.response import Response
from .models import *
class BlogList(APIView):
"""
Lists all blog posts, or creates a new post
"""
def get(self, request, format=None):
posts = Blog.objects.to_json()
return Response(posts)
Run Code Online (Sandbox Code Playgroud)
但是我收到了错误
TypeError at /blog/
__init__() takes exactly 1 argument (2 given)
Request Method: GET
Request URL: http://127.0.0.1:8000/blog/
Django Version: 1.5.1
Exception Type: TypeError …Run Code Online (Sandbox Code Playgroud) 如果我有两个序列化程序,其中一个是嵌套的,我该如何设置restore_object方法?例如,如果我定义了以下序列化程序,如何为嵌套序列化程序定义还原对象字段?从文档中可以看出如何处理这种情况并不明显.
class UserSerializer(serializers.Serializer):
first_name = serializers.CharField(required=True, max_length=30)
last_name = serializers.CharField(required=True, max_length=30)
username = serializers.CharField(required=True, max_length=30)
email = serializers.EmailField(required=True)
password = serializers.CharField(required=True)
def restore_object(self, attrs, instance=None):
if instance:
instance.first_name = attrs.get('first_name', instance.first_name)
instance.last_name = attrs.get('last_name', instance.last_name)
instance.email = attrs.get('email', instance.email)
instance.password = attrs.get('password', instance.password)
class UserProfileSerializer(serializers.Serializer):
user = UserSerializer()
bio = serializers.CharField()
def restore_object(self, attrs, instance=None):
if instance:
instance.bio = attrs.get('bio', instance.bio)
instance.user = ?????
Run Code Online (Sandbox Code Playgroud) 我有一个控制器和工厂定义如下.
myApp.controller('ListController',
function($scope, ListFactory) {
$scope.posts = ListFactory.get();
console.log($scope.posts);
});
myApp.factory('ListFactory', function($http) {
return {
get: function() {
$http.get('http://example.com/list').then(function(response) {
if (response.data.error) {
return null;
}
else {
console.log(response.data);
return response.data;
}
});
}
};
});
Run Code Online (Sandbox Code Playgroud)
令我困惑的是,我从控制器获取未定义的输出,然后控制台输出的下一行是我工厂中的对象列表.我也尝试过将控制器更改为
myApp.controller('ListController',
function($scope, ListFactory) {
ListFactory.get().then(function(data) {
$scope.posts = data;
});
console.log($scope.posts);
});
Run Code Online (Sandbox Code Playgroud)
但我收到错误
TypeError: Cannot call method 'then' of undefined
Run Code Online (Sandbox Code Playgroud)
注意:我通过http://www.benlesh.com/2013/02/angularjs-creating-service-with-http.html找到了有关使用工厂的信息.
我在python 3.3中导入Process时收到以下错误.有什么理由我会得到这样的错误,还是这个错误?我在另一个终端窗口运行django服务器,但我怀疑这与此有什么关系.
Python 3.3.2 (default, Nov 8 2013, 13:38:57)
[GCC 4.8.2 20131017 (Red Hat 4.8.2-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
# extension module loaded from '/usr/lib64/python3.3/lib-dynload/readline.cpython-33m.so'
import 'readline' # <_frozen_importlib.ExtensionFileLoader object at 0x7f8a00fc1050>
>>> from multiprocessing import Process
# ./__pycache__/multiprocessing.cpython-33.pyc matches ./multiprocessing.py
# code object from ./__pycache__/multiprocessing.cpython-33.pyc
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<frozen importlib._bootstrap>", line 1567, in _find_and_load
File "<frozen importlib._bootstrap>", line 1534, in _find_and_load_unlocked
File "<frozen …Run Code Online (Sandbox Code Playgroud) 我已经定义了一个用户配置文件模型,但希望有一个api端点将所有用户数据保存到两个模型中.我的意思是,我使用的是用户模型,我的userprofile模型定义如下
class UserProfile(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL)
biography = models.TextField()
Run Code Online (Sandbox Code Playgroud)
当我定义一个用于创建新用户的序列化程序时,如何让它将数据序列化为用户模型和userProfile模型?
如何用一定数量的有效数字表示C中的极大或极小数字.例如,如果我想在1.54334E-34上进行计算,我该怎么做呢.此外,这适用于OpenCL代码吗?
我有一个对象定义如下
class a():
@property
def prop(self):
print("hello from object.prop")
@property
def prop1(self):
print("Hello from object.prop.prop")
Run Code Online (Sandbox Code Playgroud)
我打电话的时候
>>> obj = a()
>>> obj.prop
hello from object.prop
>>> obj.prop.prop
Run Code Online (Sandbox Code Playgroud)
我收到以下回溯错误
Traceback (most recent call last):
File "object_property.py", line 13, in <module>
a.prop.prop1
AttributeError: 'NoneType' object has no attribute 'prop1'
Run Code Online (Sandbox Code Playgroud)
我想弄清楚的是我是否可以为对象定义嵌套属性?
我有一个需要多处理的脚本.我从这个脚本中发现的是多处理模块存在问题.为了测试这个理论,我复制并粘贴了
from multiprocessing import Process
def f(name):
print('hello', name)
if __name__ == '__main__':
p = Process(target=f, args=('bob',))
p.start()
p.join()
Run Code Online (Sandbox Code Playgroud)
进入测试脚本并收到以下回溯
Traceback (most recent call last):
File "a.py", line 1, in <module>
from multiprocessing import Process
File "/usr/lib64/python3.3/multiprocessing/__init__.py", line 40, in <module>
from multiprocessing.util import SUBDEBUG, SUBWARNING
File "/usr/lib64/python3.3/multiprocessing/util.py", line 16, in <module>
import threading # we want threading to install it's
File "/usr/lib64/python3.3/threading.py", line 11, in <module>
from traceback import format_exc as _format_exc
File "/usr/lib64/python3.3/traceback.py", line 3, in <module>
import …Run Code Online (Sandbox Code Playgroud) django ×4
python ×4
ajax ×1
angularjs ×1
api ×1
c ×1
factory ×1
flask ×1
html5-audio ×1
importerror ×1
mongoengine ×1
object ×1
opencl ×1
python-3.3 ×1
streaming ×1
traceback ×1