2014-07-20 63 views
2

我對Django有點新,需要一些幫助將我的python腳本(身份驗證,構建gmail服務,查詢gmail)轉換爲django應用程序。我正在嘗試使用google django樣本和他們在這裏指定的課程https://developers.google.com/api-client-library/python/guide/django,但感到有點困惑。任何幫助將不勝感激!當我訪問我的索引時,下面的代碼使我通過了認證流程,但是在點擊我的谷歌個人資料後,產生的頁面出現錯誤:"ValueError at /app/ 無法分配無:「CredentialsModel.id」不允許空值。「在Django中使用gmail API

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) 

回答

2

1)這部分是完全有問題的:

class CredentialsModel(models.Model): 
    id = models.ForeignKey(User, primary_key=True) 
    credential = CredentialsField() 

如果妳希望與用戶模型1-1關係U可以使用

user = models.OneToOneField(User, primary_key=True) 

2)U必須將此部分單獨到一個新的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 =用戶。

正確的是:

storage = Storage(CredentialsModel, 'id', request.user, 'credential') 
+0

謝謝 - 我不得不改變「ID」存儲爲「用戶」,但它看起來像它的工作。 – sacshu