python 3.x - django + tastypie: How do I POST to replace data without getting "duplicate key value violates unique constraint" -


i trying write rest api django project using tastypie. can use post data:

curl --dump-header - -h "content-type: application/json" -x post --data '{"name": "environment1", "last_active": "2015-06-18t15:56:37"}' http://localhost:8000/api/v1/report_status/ 

which works gets put database, when come send second set of data enviroment name (i.e. resend same request), intention of replace first set of data sent, following error (abbreviated):

{"error_message": "duplicate key value violates unique constraint \"<project_name>_environment_name_key\"\ndetail:  key (name)=(production) exists.\n", "traceback": "traceback ... django.db.utils.integrityerror: duplicate key value violates unique constraint \"oilserver_environment_name_key\"\ndetail:  key (name)=(production) exists. 

i understand have set environment name unique, i'm trying replace data, not upload environment same name. problem seems id auto-incrementing. not wish have provide id every time - want end user supply environment name , have replace data if in database. can tell me how done?

below relevant parts of code. have foreign key i'm not sure whether or not complicates things, or if matter entirely.

models.py:

from django.db import models   class environmentstate(models.model):     name = models.charfield(         max_length=255,         default="unknown",         help_text="current state of environment.")     description = models.textfield(         default=none,         blank=true,         null=true,         help_text="optional description state.")      def __str__(self):         return self.name   class environment(models.model):     name = models.charfield(         max_length=255,         unique=true,         help_text="name of environment")     last_active = models.datetimefield(         default=none,         blank=true,         null=true,         help_text="datetime when environment message last received.")     current_situation = models.textfield(         help_text="statement(s) giving background current env status.")     status = models.foreignkey(environmentstate)      def __str__(self):         return self.name 

resources.py:

from tastypie import fields tastypie.resources import modelresource tastypie.authorization import authorization oilserver.models import environment, environmentstate oilserver.status_checker import statuschecker   class environmentstateresource(modelresource):     class meta:         queryset = environmentstate.objects.all()         resource_name = 'environment_state'         authorization = authorization()   class reportstatusresource(modelresource):     status = fields.foreignkey(environmentstateresource, 'status',                                 null=true, full=true)      class meta:         queryset = environment.objects.all()         resource_name = 'report_status'         authorization = authorization()      def hydrate(self, bundle):         name = bundle.data.get('name')         last_active = bundle.data.get('last_active')          status_checker = statuschecker(last_active)         # statuschecker class takes in data ,          # generates 'state' (up, down) , 'situation' string explaining          # user going on.          bundle.data['current_situation'] = status_checker.situation         env_state = environmentstate.objects.get(name=status_checker.state)         bundle.data['status'] = {"pk": env_state.pk}          return bundle 

so, going wrong?

thanks

you have target single resource e.g:

http://localhost:8000/api/v1/report_status/<identifier of resource want update> 

and think need "put" request instead of "post"

so after create environment, it's id, send "put" request "http://localhost:8000/api/v1/report_status/< id > , should work.


Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

javascript - Complex json ng-repeat -

jquery - Cloning of rows and columns from the old table into the new with colSpan and rowSpan -