选择要编辑的对象

时间:2009-12-22 15:26:33

标签: python django django-views

我有一个简单的视图功能,旨在允许用户从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”。

2 个答案:

答案 0 :(得分:2)

关于get的重要事情是“得到什么?”

当你说

a=ProjectRecord.objects.get()

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

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

通常,我们将其放在网址中。

因此,您需要更新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.

答案 1 :(得分:0)

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

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

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

ProjectRecord.objects.get(pk=id)  

id是您尝试编辑的ProjectRecord的主键值。

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