2009-12-22 90 views
0

我有一個簡單的視圖功能,旨在允許用戶從html表(記錄)中列出的項目中進行選擇。點擊記錄應該將用戶轉移到他可以編輯該特定記錄的模板。代碼如下:選擇要編輯的對象

def edit_record(request): 
     if request.method == 'POST': 
       a=ProjectRecord.objects.get() 
       form = RecordForm(request.POST, instance=a) 
       if form.is_valid(): 
         form.save() 
         return HttpResponseRedirect('/') 
     else: 
       a=ProjectRecord.objects.get() 
       form = RecordForm(instance=a) 
     return render_to_response('productionModulewire.html', {'form': form}) 

問題是,只要數據庫中只有一條記錄,函數就可以很好地工作。只要我添加另一個,我會得到多個返回的項目錯誤。 我懷疑它與「objects.get()」有關,但我不知道如何正確地構造視圖?

URL是簡單的(也許太多的話):

(r'^edit/', edit_record), 

和模型是這樣的:

class ProjectRecord(models.Model): 
    client = models.CharField(max_length=50, choices=CLIENT_CHOICES) 
    account = models.CharField(max_length=50, choices=ACCOUNT_CHOICES) 
    project_type = models.CharField(max_length=50, choices=TYPE_CHOICES) 
    market = models.CharField(max_length=50, choices=MARKET_CHOICES) 
    agencyID = models.CharField(max_length=30, unique=True, blank=True, null=True) 
    clientID = models.CharField(max_length=30, unique=True, blank=True, null=True) 
    prjmanager = models.CharField(max_length=64, unique=False, blank=True, null=True) 
    acclead = models.CharField(max_length=64, unique=False, blank=True, null=True) 
    artdirector = models.CharField(max_length=64, unique=False, blank=True, null=True) 
    prdlead = models.CharField(max_length=64, unique=False, blank=True, null=True) 
    intlead = models.CharField(max_length=64, unique=False, blank=True, null=True) 
    prjname = models.CharField(max_length=200, unique=True) 
    prjstatus = models.CharField(max_length=50, choices=STATUS_CHOICES) 
    as_of = models.DateField(auto_now_add=False) 
    format = models.CharField(max_length=64, unique=False, blank=True, null=True) 
    target_studio = models.DateField(unique=False, blank=True, null=True) 
    mech_return = models.DateField(unique=False, blank=True, null=True) 
    comp_return = models.DateField(unique=False, blank=True, null=True) 
    target_release = models.DateField(unique=False, blank=True, null=True) 
    record_added = models.DateField(auto_now_add=True) 
    record_modified = models.DateTimeField() 
    studio_name = models.CharField(max_length=64, unique=False, blank=True, null=True) 
    studio_process = models.CharField(max_length=64, unique=False, blank=True, null=True, choices=PROCESS_CHOICES) 
    to_studio = models.DateTimeField(unique=False, blank=True, null=True) 
    from_studio = models.DateTimeField(unique=False, blank=True, null=True) 
    studio_name2 = models.CharField(max_length=64, unique=False, blank=True, null=True) 
    studio_process2 = models.CharField(max_length=64, unique=False, blank=True, null=True, choices=PROCESS_CHOICES) 
    to_studio2 = models.DateTimeField(unique=False, blank=True, null=True) 
    from_studio2 = models.DateTimeField(unique=False, blank=True, null=True) 
    comments = models.TextField(max_length=500, unique=False, blank=True, null=True) 
    summary = models.TextField(max_length=500, unique=False, blank=True, null=True) 
    upload_pdf = models.CharField(max_length=50, unique=False, blank=True, null=True) 
    upload_achive = models.CharField(max_length=50, unique=False, blank=True, null=True) 

    def __unicode__(self): 
     return u'%s' % self.prjname 

    class Admin: 
     pass 

從雛型 「RecordForm」 推導。

+0

內置的管理界面完全自動爲您完成此而無需編寫任何代碼。爲什麼當你已經在管理界面中正常工作時,你自己寫這個交易? – 2009-12-22 16:49:55

+0

因爲我試圖爲相當數量的最終用戶定製環境。例如,這個特定的記錄數據庫將只是許多可以由用戶調用的「獨立」模塊中的一個。建議的功能要求用戶能夠同時打開和處理多個「模塊」。 – kjarsenal 2009-12-22 21:03:48

回答

2

關於get的重要事情是「得到什麼?」

當你說

a=ProjectRecord.objects.get() 

你忽視提供任何選擇標準。你想從數據庫中選擇哪一行?

哪一行?嗯...... GET交易如何知道哪一行將被編輯?

通常,我們把它放在URL中。

因此,您需要更新您的urls.py以在URL路徑中包含記錄標識。你需要更新你的視圖函數定義來接受這個記錄ID。最後,您需要更新GET和POST以使用來自URL的此記錄標識。


更新urls.py包含對象ID。見http://docs.djangoproject.com/en/1.1/topics/http/urls/#named-groups

urlpatterns = patterns('', 
    (r'^class/(?P<object_id>\d+?)/$', 'app.views.edit_record'), 

更新您的視圖功能

def edit_record(request, object_id = None): 
    if request.method == "POST": 
     if object_id is None: 
      return Http_404 
     ProjectRecord.objects.get(pk = int(object_id)) 

    etc. 
+0

不知道通過url傳遞值(我知道......該死的新手:-)。該地區的一項研究產生了黃金!感謝你的幫助。非常感激。 – kjarsenal 2009-12-22 17:00:05

+0

@kjarsenal:一定要接受一個幫助你的答案。 – 2009-12-22 18:15:30

+0

我剛剛點擊左上方的複選標記。我認爲這就是所要求的。謝謝。 – kjarsenal 2009-12-22 21:05:26

0

沒有更多的信息,很難告訴你需要改變什麼,但是你的猜測是正確的,問題出在你的ProjectRecord.objects.get()調用上。

您應該傳遞某種信息才能將列表限制爲一個。

在大多數情況下,你將需要:

ProjectRecord.objects.get(pk=id) 

id是的ProjectRecord您要編輯的主鍵值。

您能展示您的urls.py的相關代碼以及關於您的ProjectRecord模型的更多信息嗎?

+0

謝謝大家的幫助。我編輯了原始帖子,以便它包含更多代碼。希望能幫助到你。 – kjarsenal 2009-12-22 16:12:44