Python not throwing exception when trying to update an attribute that is not available in the model

StackOverflow https://stackoverflow.com/questions/22682306

Question

Am using python 4.3 and django 1.0.2. When I try to update a field that is not available in the model no exception is raised.
Eg x = Mymodel.objects.get(att1='abcd') x.notvalid <---- raises an exception x.notvalid = 'xxx' <--- not raising exception

Can someone explain how this works?

Was it helpful?

Solution

If you do an assignment:

x.notvalid = 'xxx'

You're just assigning xxx to x.notvalid. It is just python, it's like:

some_var = 'xxx'

Only you're doing it on the object x. In this case, x is a Django model, but it's also still a python object, and you can assign new properties to a python object like that.

If you try to access x.notvalid before you've assigned anything to it, it will logically throw an exeption, because x.notvalid is not assigned yet.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top