samedi 29 novembre 2014

Python 2.7 - Django reproduisant un modèle objet causant problème - Stack Overflow


I have a 2 models with a foreign/Primary key to same model.


model Foo:
FK(Too, pk)
model Coo:
FK(Too, pk)
model Too:
blah = charfield()

In the views I am seeing some very strange behavior. I think I am doing something very wrong. I want to replicate a object of Too and then save it. For e.g.


too = Too.create(blah="Awesome")
too.save()
foo = Foo.create(too=too)
foo.save()
too.id = None #Copy the original
too.save()
coo = Coo.create(too=too)
coo.save()
print foo.too.id
print coo.too.id
#above 2 print statements give same id

When I check in the admin the both foo and coo have different too object saved. But while printing it is showing the same. Why is that happening. I think I am doing something fundamentally wrong.






Django looks at the primary key to determine uniqueness, so work with that directly:



too.pk = None
too.save()

Setting the primary key to None will cause Django to perform an INSERT, saving a new instance of the model, rather than an UPDATE to the existing instance.





Source:
http://stackoverflow.com/a/4736172/1533388


UPDATE: err, using pk and id are interchangeable in this case, so you'll get the same result. My first answer didn't address your question.


The discrepancy here is between what is occurring in python vs. what can be reconstituted from the database.


Your code causes Django to save two unique objects to the database, but you're only working with one python Too instance. When foo.save() occurs, the database entry for 'foo' is created with a reference to the DB entry for the first Too object. When coo.save() occurs, the database entry for 'coo' is created, pointing to the second, unique Too object that was stored via:


too.id = None #Copy the original 
too.save()

However, in python, both coo and foo refer to the same object, named 'too', via their respective '.too' attributes. In python, there is only one 'Too' instance. So when you update too.id, you're updating one object, referred to by both coo and foo.


Only when the models are reconstituted from the database (as the admin view does in order to display them) are unique instances created for each foreign key; this is why the admin view shows two unique saved instances.



I have a 2 models with a foreign/Primary key to same model.


model Foo:
FK(Too, pk)
model Coo:
FK(Too, pk)
model Too:
blah = charfield()

In the views I am seeing some very strange behavior. I think I am doing something very wrong. I want to replicate a object of Too and then save it. For e.g.


too = Too.create(blah="Awesome")
too.save()
foo = Foo.create(too=too)
foo.save()
too.id = None #Copy the original
too.save()
coo = Coo.create(too=too)
coo.save()
print foo.too.id
print coo.too.id
#above 2 print statements give same id

When I check in the admin the both foo and coo have different too object saved. But while printing it is showing the same. Why is that happening. I think I am doing something fundamentally wrong.





Django looks at the primary key to determine uniqueness, so work with that directly:



too.pk = None
too.save()

Setting the primary key to None will cause Django to perform an INSERT, saving a new instance of the model, rather than an UPDATE to the existing instance.





Source:
http://stackoverflow.com/a/4736172/1533388


UPDATE: err, using pk and id are interchangeable in this case, so you'll get the same result. My first answer didn't address your question.


The discrepancy here is between what is occurring in python vs. what can be reconstituted from the database.


Your code causes Django to save two unique objects to the database, but you're only working with one python Too instance. When foo.save() occurs, the database entry for 'foo' is created with a reference to the DB entry for the first Too object. When coo.save() occurs, the database entry for 'coo' is created, pointing to the second, unique Too object that was stored via:


too.id = None #Copy the original 
too.save()

However, in python, both coo and foo refer to the same object, named 'too', via their respective '.too' attributes. In python, there is only one 'Too' instance. So when you update too.id, you're updating one object, referred to by both coo and foo.


Only when the models are reconstituted from the database (as the admin view does in order to display them) are unique instances created for each foreign key; this is why the admin view shows two unique saved instances.


0 commentaires:

Enregistrer un commentaire