2013-04-20 23 views
2

我有一個創建對象的項目,我想使用uuids鍵入對象。當我在manage.py中使用簡單的Django測試服務器運行此項目時,一切都很好。然而我正在嘗試部署到heroku,所以我已經建立了一個virtualenv與一個gunicorn服務器,打破了我的代碼。我已將錯誤追溯到由於某種原因,與此服務器一起運行時,我的UploadedFile對象始終具有空白的訪問鏈接。在virtualenv中將Django項目切換到gunicorn服務器時出錯

這裏是我的models.py代碼:

from django.db import models 
import uuid 
import datetime 

# UUID field will be used to key file upload objects 
class UUIDField(models.CharField) : 
    def __init__(self, *args, **kwargs): 
    kwargs['max_length'] = kwargs.get('max_length', 64) 
    kwargs['blank'] = True 
    models.CharField.__init__(self, *args, **kwargs) 

class UploadedFile(models.Model): 
    #accessLink = UUIDField(primary_key=True, editable=False) 
    accessLink = models.CharField(primary_key=True, max_length=64) 
    uploadTime = models.DateTimeField(default=datetime.datetime.now) 
    filename = models.CharField(max_length=200) 

    def __init__(self, *args, **kwargs): 
    super(UploadedFile, self).__init__(*args, **kwargs) 
    if self.accessLink is '': 
     self.accessLink = str(uuid.uuid4()) 

    def __unicode__(self): 
    return filename 

這裏是包含在views.py我的指數代碼:

from django.http import HttpResponse # Just for lulz            
from django.shortcuts import render 
from django.core.context_processors import csrf 
from django.shortcuts import render_to_response 
from django.template import Template, Context 
from smartfile import BasicClient 
from filemapper.models import UploadedFile 

def index(request): 
    if request.method == 'POST': 
    c = {} 
    c.update(csrf(request)) 
    authKey = 'ggcCEFzGBcJYAQSHNf7AnF8r7c03cB' 
    authPassword = 'CJlxZHCocieiPOKuhI6GdGOwwTMr2i' 
    api = BasicClient(authKey, authPassword) 
    for f in request.FILES.values(): 
     # Create our record of the file 
     u = UploadedFile(filename=f.name) 
     u.save() 
     # Create a directory on smartfile 
     api.post('/path/oper/mkdir/', path=u.accessLink) 
     # Upload the file to s 
     api.post('/path/data/' + u.accessLink, file=f.file, name=u.filename) 
     # This page should display how to access the uploade 
     return generate(request, u.accessLink) 
    else: 
     return HttpResponse('File not found') 
    else: 
    return render(request, 'filemapper/index.html') 

的代碼失敗,當我嘗試發佈一個文件添加到索引,因爲u.accessLink不是正確形成的UUID,它是一個空白字符串。

回答

0

有幾件事情在這裏,我作爲一個Django開發人員做的其他方式:

  • 也無需對碼UUIDField,只需使用一個char場max_lengthblank=True
  • 唐「T重寫UploadedFile.__init__ - 設置accessLink在UploadedFile.save()方法

此外,重載新樣式類方法時,你應該使用super,不叫__init__方法明確。

相關問題