Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django save model ForeignKey relation

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.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....

like image 691
aragon Avatar asked Feb 17 '11 15:02

aragon


People also ask

How does Django store ForeignKey values?

Note that the _id in the artist parameter, Django stores foreign keys id in a field formed by field_name plus _id so you can pass the foreign key id directly to that field without having to go to the database again to get the artist object.

What is the difference between ForeignKey and OneToOneField?

A one-to-one relationship. Conceptually, this is similar to a ForeignKey with unique=True , but the "reverse" side of the relation will directly return a single object. In contrast to the OneToOneField "reverse" relation, a ForeignKey "reverse" relation returns a QuerySet .

What is ForeignKey Django models?

Introduction to Django Foreign Key. A foreign key is a process through which the fields of one table can be used in another table flexibly. So, two different tables can be easily linked by means of the foreign key. This linking of the two tables can be easily achieved by means of foreign key processes.

Can a model have two foreign keys Django?

Your intermediate model must contain one - and only one - foreign key to the source model (this would be Group in our example), or you must explicitly specify the foreign keys Django should use for the relationship using ManyToManyField.


2 Answers

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", "")
like image 163
hendrixski Avatar answered Oct 05 '22 01:10

hendrixski


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()
like image 29
Yuji 'Tomita' Tomita Avatar answered Oct 05 '22 00:10

Yuji 'Tomita' Tomita