我对Django有点新意,需要一些帮助将我的python脚本(验证,构建gmail服务,查询gmail)翻译成django应用程序。我试图使用google django示例和他们在此处指定的课程https://developers.google.com/api-client-library/python/guide/django,但有点困惑。任何帮助将不胜感激!当我访问我的索引时,下面的代码将引导我完成身份验证流程,但在点击我的Google个人资料后,生成的页面出错:"ValueError at /app/
无法分配无:" CredentialsModel.id"不允许空值。"
Traceback:
File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in get_response
112. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/sachinshukla/Desktop/gmail/customer_support/views.py" in index
70. credentials = run(flow, storage, http=http)
File "/Library/Python/2.7/site-packages/oauth2client/util.py" in positional_wrapper
132. return wrapped(*args, **kwargs)
File "/Library/Python/2.7/site-packages/oauth2client/old_run.py" in run
156. storage.put(credential)
File "/Library/Python/2.7/site-packages/oauth2client/client.py" in put
325. self.locked_put(credentials)
File "/Library/Python/2.7/site-packages/oauth2client/django_orm.py" in locked_put
126. entity = self.model_class(**args)
File "/Library/Python/2.7/site-packages/django/db/models/base.py" in __init__
405. setattr(self, field.name, rel_obj)
File "/Library/Python/2.7/site-packages/django/db/models/fields/related.py" in __set__
335. (instance._meta.object_name, self.field.name))
Exception Type: ValueError at /customer_support/
Exception Value: Cannot assign None: "CredentialsModel.id" does not allow null values.
观点代码:
CLIENT_SECRETS = os.path.join(os.path.dirname(__file__), '..', 'client_secret.json')
REDIRECT_URI = 'http://localhost:8080/'
SCOPES = [
'https://www.googleapis.com/auth/gmail.modify',
]
def index(request):
storage = Storage(CredentialsModel, 'id', request.user.id, 'credential')
credentials = storage.get()
flow = flow_from_clientsecrets(CLIENT_SECRETS, scope=SCOPES)
http = httplib2.Http()
if credentials is None or credentials.invalid:
credentials = run(flow, storage, http=http)
# Authorize the httplib2.Http object with our credentials
http = credentials.authorize(http)
# Build the Gmail service from discovery
gmail_service = build('gmail', 'v1', http=http)
# then do something, query threads, messages, etc,
and return them to a template - not sure where exactly to put the methods for querying threads, messages, etc.
型号:
from django.db import models
import pickle
import base64
from django.contrib import admin
from django.contrib.auth.models import User
from django.db import models
from oauth2client.django_orm import FlowField
from oauth2client.django_orm import CredentialsField
class CredentialsModel(models.Model):
id = models.ForeignKey(User, primary_key=True)
credential = CredentialsField()
class CredentialsAdmin(admin.ModelAdmin):
pass
admin.site.register(CredentialsModel, CredentialsAdmin)
答案 0 :(得分:2)
1)这部分完全成问题:
class CredentialsModel(models.Model):
id = models.ForeignKey(User, primary_key=True)
credential = CredentialsField()
如果你想与用户模型建立1-1关系,你可以使用
user = models.OneToOneField(User, primary_key=True)
2)你必须将这个部分分成一个新的admin.py文件,以避免将来出现问题。
class CredentialsAdmin(admin.ModelAdmin):
pass
admin.site.register(CredentialsModel, CredentialsAdmin)
https://docs.djangoproject.com/en/1.6/topics/db/examples/one_to_one/
3)问题来自:
storage = Storage(CredentialsModel, 'id', request.user.id, 'credential')
user_id!= user。
正确的是:
storage = Storage(CredentialsModel, 'id', request.user, 'credential')