But I know the problem and is ForeignKey to model listurl because if I
use
another model without foreigkey everything starts working
Excuse my bad English
I know the question is stupid but I do not understand what mistake
Thanks
###################################################################
Traceback (most recent call last):
File "/home/lupux/DevelDir/DJANGO/siteupdate/../siteupdate/api/
handlers.py", line 28, in create
urlip=attrs['urlip'])
File "/usr/lib/python2.5/site-packages/django/db/models/base.py",
line 311, in __init__
setattr(self, field.name, rel_obj)
File "/usr/lib/python2.5/site-packages/django/db/models/fields/
related.py", line 273, in __set__
self.field.name, self.field.rel.to._meta.object_name))
ValueError: Cannot assign "u'servicetype'": "listurl.servicetype" must
be a "servicetype" instance.
* Closing connection #0
####################################################
Snip code handler.py
from piston.handler import BaseHandler, AnonymousBaseHandler
from piston.utils import rc, require_mime, require_extended
import re
from siteupdate.proxy.models import
listurlForm,listurl,hostserver,servicetype
class listurlHandler(BaseHandler):
model = listurl
anonymous = 'AnonymouslisturlHandler'
fields = ('urlip','hostserver','servicetype')
def create(self, request):
"""
Creates a new blogpost.
"""
attrs = self.flatten_dict(request.POST)
print rc.CREATED
if self.exists(**attrs):
print "DUCLICATO"
print attrs
return rc.DUPLICATE_ENTRY
else:
print "NUOVO RECORD"
inspl=listurl(hostserver=attrs['hostserver'],
servicetype=attrs['servicetype'],
urlip=attrs['urlip'])
return inspl
##############################################################
Snip code model.py
from django.db import models
from django.forms import ModelForm
#Table define listurl for service and host
class servicetype(models.Model):
servicetype=models.CharField(max_length=200)
descr=models.CharField(max_length=200)
def __unicode__(self):
return self.servicetype
class servicetypeForm(ModelForm):
class Meta:
model = servicetype
#Table define hostserver referenze
class hostserver(models.Model):
hostserver=models.CharField(max_length=200)
aliasurl=models.CharField(max_length=200)
def __unicode__(self):
return self.hostserver
class hostserverForm(ModelForm):
class Meta:
model = hostserver
class listurl(models.Model):
servicetype =
models.ForeignKey('servicetype',verbose_name="Servizio squid/
dansguardian")
hostserver =
models.ForeignKey('hostserver',verbose_name="Server associato")
urlip=models.CharField(max_length=200,verbose_name="domino/ip
da gestire")
def __unicode__(self):
return self.urlip
#def get_absolute_url(self):
# return self.urlip
def get_absolute_url(self):
return self.urlip
#Table type service squid/danguardian
class listurlForm(ModelForm):
class Meta:
model = listurl
#################################################
On 10 Feb, 14:19, luupux <luu...@gmail.com> wrote:
> Hello i have a problem with simple application , but i not understand
> where is the my error.
>
> But I know the problem and is ForeignKey to model listurl because if I
> use
> another model without foreigkey everything starts working
> Excuse my bad English
> I know the question is stupid but I do not understand what mistake
> Thanks
> ###################################################################
> Traceback (most recent call last):
>
> File "/home/lupux/DevelDir/DJANGO/siteupdate/../siteupdate/api/handlers.py", line28, in create
> urlip=attrs['urlip'])
>
> File "/usr/lib/python2.5/site-packages/django/db/models/base.py",line311, in __init__
> setattr(self, field.name, rel_obj)
>
> File "/usr/lib/python2.5/site-packages/django/db/models/fields/related.py", line273, in __set__
> self.field.name, self.field.rel.to._meta.object_name))
>
> ValueError: Cannot assign "u'servicetype'": "listurl.servicetype" must
> be a "servicetype" instance.* Closing connection #0
value in handlery.py as a string but foreginkey as method
I do not know how to use a field in foreginkey in def create .
excuse my bad English
Model.py
class listurl(models.Model):
servicetypelist = models.ForeignKey('servicetype')
urlip=models.CharField(max_length=200)
hander.py
listurl(servicetypelist=attrs["value"],urlip=attrs['urlip'])
attrs["value"] = is string and not good because servicetypelist
not a string but foreginkey method
im a newbye
2010/2/19 Stefan Foulis <stefan...@gmail.com>:
> I'm having the same problem. I'm confused about how I can change the
> value of a Model field that is a ForeignKey in a PUT or POST request.
>
> --
> You received this message because you are subscribed to the Google Groups "django-piston" group.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to django-pisto...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/django-piston?hl=en.
>
>
see http://bitbucket.org/yml/django-piston/src/72c72e0b4b7e/piston/handler.py#cl-106
(create)
and http://bitbucket.org/yml/django-piston/src/72c72e0b4b7e/piston/handler.py#cl-131
(update)
For my particular use case I did an override of the create method on
the handler (see http://dpaste.com/hold/161697/) and added special
treatment for the ForeignKey I'm interested in ('myfkfieldname'). And
I did something similar for the update method.
This could be extended to actually look in _meta of the model to
evaluate all ForeignKeys automatically.
Isn't there a security issue here? No matter what is in `fields` on
the handler, if the client has update permissions on a model it can
change all of the fields on the model.
--
Stefan
the code and some preliminary explanations of it are here:
http://blog.bennyland.com/2010/03/17/extending-pistons-basehandler-for-foreignkey-support/