使用 ModelForms 时如何更新已存在的行?
我有一个问题,当其中一个字段是我的主键时,如何更新数据库中的现有行。我正在使用 ModelForm 和 Django-Piston - 我的主要目标是将 RESTful Post 发送到我的网络服务。我能够正确发送初始帖子(即主键值尚不存在)。问题是当我想更新主键已存在的值时 - 当我发出 f.is_valid() 时它会失败,因为“此 UniqueIdentifier 已存在”。如何使用 ModelForms 进行表单验证以更新现有行?
models.py:forms.py
from django.db import models
class DeviceModel(models.Model):
uniqueIdentifier = models.CharField(primary_key=True, max_length=100)
deviceToken = models.CharField(max_length=100)
我的
from django import forms
from models import DeviceModel
class DeviceModelForm(forms.ModelForm):
class Meta:
model = DeviceModel
handlers.py
class DeviceHandler(BaseHandler):
allowed_methods = ('POST', 'GET', 'DELETE',)
def create(self, request):
f = DeviceModelForm(request.POST)
if f.is_valid():
new_object = f.save()
return new_object
return rc.BAD_REQUEST
urls.py
from django.conf.urls.defaults import *
from piston.resource import Resource
from api.handlers import DeviceHandler
device_handler = Resource(DeviceHandler)
urlpatterns = patterns('',
(r'^api/$', device_handler, {'emitter_format': 'json'}),
)
I have a question on how to update an existing row in my database when one of the fields is my primary key. I am using ModelForm and Django-Piston - my main goal here is to have RESTful Post send to my webservice. I am able to have initial Posts be sent correctly (i.e. that Primary key value doesn't exist yet). The problem is when I want to update a value where the Primary key already exists - when I issue a f.is_valid() it fails because "this UniqueIdentifier already exists". How can I do form validation using ModelForms to update an existing row?
My models.py:
from django.db import models
class DeviceModel(models.Model):
uniqueIdentifier = models.CharField(primary_key=True, max_length=100)
deviceToken = models.CharField(max_length=100)
forms.py
from django import forms
from models import DeviceModel
class DeviceModelForm(forms.ModelForm):
class Meta:
model = DeviceModel
handlers.py
class DeviceHandler(BaseHandler):
allowed_methods = ('POST', 'GET', 'DELETE',)
def create(self, request):
f = DeviceModelForm(request.POST)
if f.is_valid():
new_object = f.save()
return new_object
return rc.BAD_REQUEST
urls.py
from django.conf.urls.defaults import *
from piston.resource import Resource
from api.handlers import DeviceHandler
device_handler = Resource(DeviceHandler)
urlpatterns = patterns('',
(r'^api/
, device_handler, {'emitter_format': 'json'}),
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
django 文档 给出了如何创建“表单”的简单示例更改现有的 [[entity]]”:
如果您想使用相同的流程来插入新对象和更改现有对象,则必须根据查找主键是否成功来单独实例化表单(现有对象)或失败(新对象)!-)
The django docs give a simple example of how to create "a form to change an existing [[entity]]":
If as it seems you want to use the same flow both for inserting new objects and changing existing ones, you'll have to instantiate the form separately depending on whether looking for the primary key succeeds (existing object) or fails (new object)!-)
要更新现有行(或 ORM 中的对象),您必须告诉 ModelForm 在实例化它时使用什么实例:
我不确定您从哪里获得 myobject > 虽然使用活塞,但你的问题似乎暗示你已经解决了这个特定问题。
To update an existing row (or object in ORM-speak), you have to tell the
ModelForm
what instance to use when instantiating it:I'm not sure where you get
myobject
from using piston, though, but your question seems to imply that you solved that particular problem already.这是一个更完整的解决方案,不使用任何基于类的视图,汇集了此页面上的其他答案和评论。
我把它作为对 jquery ajax 的回复。
Here is a more complete solution not using any Class based views, bringing together the other answers and comments on this page.
I have it working as a reply to a jquery ajax.
这是我根据实体是否存在进行更新或创建的操作:
This is what I did to update or create depending on if the entity exists: