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})

问题是该函数仅在数据库中只有 1 条记录时才能完美运行。一旦我添加另一个,我就会收到一个多次返回的项目错误。我怀疑它与“objects.get()”有关,但我不知道如何正确构建视图?

网址很简单(也许太多了):

(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”由此衍生而来。

4

2 回答 2

2

重要的get是“得到什么?”

当你说

a=ProjectRecord.objects.get()

您忽略了提供任何选择标准。您想从数据库中获取哪一行?

哪一排?嗯……事务如何GET知道要编辑哪一行?

通常,我们将其放在 URL 中。

因此,您需要更新您的urls.py以在 URL 路径中包含记录 ID。您需要更新视图函数定义以接受此记录 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.
于 2009-12-22T15:32:48.170 回答
0

如果没有更多信息,很难说出您需要更改什么,但您的猜测是正确的,问题出在您的ProjectRecord.objects.get()电话上。

您应该传递某种信息以将列表限制为一个。

在大多数情况下,您将需要:

ProjectRecord.objects.get(pk=id)  

您尝试编辑id的主键值在哪里。ProjectRecord

您能否显示您的相关代码urls.py以及有关您的ProjectRecord模型的更多信息?

于 2009-12-22T15:32:17.623 回答