If I use
setattr(p,'wrong_attr','value')
instead of
p=MyModel.objects.filter(WRONG_ATTRIBUTE=value)
I don't get the error related to the non existent attribute. Is there a way to get that error using setattr?
Thank you.
If I use
setattr(p,'wrong_attr','value')
instead of
p=MyModel.objects.filter(WRONG_ATTRIBUTE=value)
I don't get the error related to the non existent attribute. Is there a way to get that error using setattr?
Thank you.
These two commands are not in any way related. The equivalent of your setattr
call is this:
p.wrong_attr = 'value'
which does not raise an error either, as Django model instances are just Python objects, and Python objects allow you to set attributes arbitrarily.
If you're asking how you can find whether "wrong_attr" is a valid field name, you can look in some of the methods of p._meta
, such as get_all_field_names()
.