#12279: prepare_database_save in add_update_fields makes some custom fields be impossible. ---------------------------------------------------+------------------------ Reporter: bear330 | Owner: nobody Status: new | Milestone: Component: Database layer (models, ORM) | Version: 1.1 Resolution: | Keywords: prepare_database_save, ObjectField, ClassField Stage: Unreviewed | Has_patch: 0 Needs_docs: 0 | Needs_tests: 0 Needs_better_patch: 0 | ---------------------------------------------------+------------------------ Comment (by bear330):
OK, for the ObjectField and ClassField, I can use it in models like this: {{{ #!python # This is just a example, no meaning. class MyModel(models.Model): obj = ObjectField() clz = ClassField() }}} {{{ #!python my = MyModel() my.obj = User.object.all()[0] my.clz = User my.save() # This is OK. my.obj = User.object.all()[-1] # Change another user. my.save() # Update it, but it will raise error because User (all models) has prepare_database_save attribute, so the code in add_update_fields method (subqueries.py): # if hasattr(val, 'prepare_database_save'): # val = val.prepare_database_save(field) # else: # val = field.get_db_prep_save(val) # # val (user object) will be changed and I can't do anything before that. # So, I do some hackish in ObjectField above to make this work. # But for ClassField, this is no good way to do that. my.clz = Profile my.save() # It will raise error, because hasattr(val, 'prepare_database_save') is True, # it will call val = val.prepare_database_save(field) # then: # TypeError: unbound method prepare_database_save() must be called with Profile instance as first argument. }}} This is why I said that is impossible do to this kind of custom field. Thanks! -- Ticket URL: <http://code.djangoproject.com/ticket/12279#comment:2> Django <http://code.djangoproject.com/> The Web framework for perfectionists with deadlines. -- You received this message because you are subscribed to the Google Groups "Django updates" group. To post to this group, send email to django-upda...@googlegroups.com. To unsubscribe from this group, send email to django-updates+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-updates?hl=en.