in my model.py
class Layer(models.Model):
user = models.IntegerField()
name = models
...
class Point(models.Model):
layers = models.ForeignKey(Layer)
meta = models.TextField()
...
in my view.py
def datasave(request, id):
mid = request.POST.get("layerid",default = "")
metas = request.开发者_Python百科POST.get("meta",default = "")
cs = Point()
cs.layers = mid
cs.meta = metas
cs.save()
but it gives an error in my django debug..in my project i use geodjango,openlayers and extjs... i didnt find any solution about saving my post
i didnt make any relation with my foreignkey.. basically i want to make a layer than when i want to add a point in my layer , i want save my point with layer id....
A more efficient way is to specify the foreign key by adding an "_id" at the end, like this:
cs = Point(layers_id = mid, meta = metas)
cs.save()
DO NOT do layers=XXX.objects.get(id=###) because that's doubling the number of database queries.
ALSO ... you're not cleaning your POST data here, which is pretty dangerous. Get it like this:
from django import forms
id_field = forms.IntegerField()
layer_id = id_field.clean(request.POST.get("layerId", "")
It's helpful to post the traceback to help understand your problem (for future questions).
It looks to me like you are passing in a number to your Point
instance's layers
attribute which would cause a ValueError
. The only place you can pass a number to a foreign key field is in a django form which does the lookup for you.
You need to assign an actual Layer
instance instead.
cs = Point()
cs.layers = Layer.objects.get(id=mid)
cs.meta = metas
cs.save()
精彩评论