开发者

Update a db record in DJANGO with out having to use a form? EXAMPLE: Click to complete UPDATES a record

I have an item in a list. I want to click something and have that click trigger a view. That view updates that list item's status in the db.

For example, add to cart, or save the date, or completed.

In each case, clicking that link will update the db record with a new status. Right now these are just boolean fields.

You can see where this is going. A URL tied to that view will be called with Ajax. But I'm trying to do this with graceful degradation. I know how to do the Ajax part, just not the db update part.

I've never done much more than the basic form.is_valid and form.save. So I'm a little lost.

Ideas welcome.

开发者_C百科

Thanks


Something like:

def some_view(request, *args, **kwargs):
    my_obj = MyModel.object.filter(pk=kwargs['id'])
    if my_obj:
        my_obj = my_obj[0]
        my_obj.fancy_status = "someone wants to buy me!"
        my_obj.save()
    return some_appropriate_response()

with a url pattern like

(r'^/my_objects/(?P<id>\d{1,9})/buy/$', some_view)

seems to be what you're looking for, right?

Basically, you can save instances of your models just like you might think.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜