开发者

select object to edit

I have a simple view function that's designed to allow the user to choose from items listed in an html table (records). Clicking on a record should divert the user to the template from which he can edit that specific record. The code is as follows:

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

The problem is that the function works perfectly well ONLY so long as there is only 1 record in the开发者_开发问答 database. As soon as I add another, I get a multiple returned item error. I suspect it has something to do with "objects.get()" but I don't know how to correctly structure the view?

The url is simple (perhaps too much so):

(r'^edit/', edit_record),

and the model looks like this:

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

from which the model form "RecordForm" was derived.


The important thing about get is "get what?"

When you say

a=ProjectRecord.objects.get()

you neglected to provide any selection criteria. Which row do you want from the database?

Which row? Hmmmm... How does the GET transaction know which row is going to be edited?

Usually, we put that in the URL.

So, you'll need to update your urls.py to include the record ID on the URL path. You'll need to update your view function definition to accept this record ID. Finally, you'll need to update GET and POST to use this record identification which came from the URL.


Update urls.py to include the object id. See http://docs.djangoproject.com/en/1.1/topics/http/urls/#named-groups

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

Update your view function

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.


Without more information it is hard to tell what you need to change, but your guess is correct, the problem is with your ProjectRecord.objects.get() call.

You should be passing some sort of information to get in order to limit the list down to one.

In most cases, you will need:

ProjectRecord.objects.get(pk=id)  

Where id is the primary key value of the ProjectRecord you are trying to edit.

Could you show the relevant code from your urls.py as well as more information on your ProjectRecord model?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜