On Nov 22, 4:47 pm, Micah Vivion <micahviv...@gmail.com> wrote: > Greetings, > > 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 - f.is_valid() it fails because "this UniqueIdentifier > already exists". How can I do form validation using ModelForms to update an > existing row?
You want to use the instance argument to the ModelForm constructor: http://docs.djangoproject.com/en/1.1/topics/forms/modelforms/#the-save-method -Preston > > 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'}), > ) -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=.