﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
19427	Lacking Documentation on Custom Related Fields Pre-Save Override	s.shanabrook@…		"The process to create a custom field that inherits a `OneToOneField` or a `ForeignKey ` (and I assume a `ManyToManyField` as well) and overrides the `pre_save` method, is not clear in the documentation. [https://docs.djangoproject.com/en/dev/howto/custom-model-fields/#preprocessing-values-before-saving The section] that outlines how to override the `pre_save` method says that the attribute name is `self.attname`, when in fact on a related field, `self.attname` is the model's id field for the related object.

So in order to actually modify the related object and then save it, you must do something like this:

       
{{{
#!python
class CustomRelatedField(ForeignKey):
    def pre_save(self, model_instance, add):
        related_object = getattr(model_instance, self.name)
        # Do stuff with object, custom saving

        id = getattr(related_object, 'id', None)
        setattr(model_instance, self.attname, id)
        setattr(model_instance, self.name, redirect)
        return id
}}}"	Cleanup/optimization	closed	Documentation	1.4	Normal	wontfix			Accepted	0	0	0	0	0	0
