[Django] #19427: Lacking Documentation on Custom Related Fields Pre-Save Override

18 views
Skip to first unread message

Django

unread,
Dec 4, 2012, 10:13:21 PM12/4/12
to django-...@googlegroups.com
#19427: Lacking Documentation on Custom Related Fields Pre-Save Override
--------------------------------+--------------------
Reporter: s.shanabrook@… | Owner: nobody
Type: Bug | Status: new
Component: Documentation | Version: 1.4
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 1 | UI/UX: 0
--------------------------------+--------------------
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
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/19427>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Dec 4, 2012, 10:14:11 PM12/4/12
to django-...@googlegroups.com
#19427: Lacking Documentation on Custom Related Fields Pre-Save Override
--------------------------------+--------------------------------------

Reporter: s.shanabrook@… | Owner: nobody
Type: Bug | Status: new
Component: Documentation | Version: 1.4
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 1
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 1 | UI/UX: 0
--------------------------------+--------------------------------------
Changes (by s.shanabrook@…):

* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/19427#comment:1>

Django

unread,
Dec 20, 2012, 12:42:05 AM12/20/12
to django-...@googlegroups.com
#19427: Lacking Documentation on Custom Related Fields Pre-Save Override
--------------------------------------+------------------------------------
Reporter: s.shanabrook@… | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Documentation | Version: 1.4
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 0 | Needs documentation: 1
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 1 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by russellm):

* type: Bug => Cleanup/optimization
* stage: Unreviewed => Accepted


--
Ticket URL: <https://code.djangoproject.com/ticket/19427#comment:2>

Django

unread,
Jan 1, 2013, 2:07:06 PM1/1/13
to django-...@googlegroups.com
#19427: Lacking Documentation on Custom Related Fields Pre-Save Override
-------------------------------------+-------------------------------------
Reporter: s.shanabrook@… | Owner:
Type: | vijay_shanker
Cleanup/optimization | Status: assigned
Component: Documentation | Version: 1.4

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 1
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by vijay_shanker):

* owner: nobody => vijay_shanker
* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/19427#comment:3>

Django

unread,
Feb 13, 2013, 3:57:01 AM2/13/13
to django-...@googlegroups.com
#19427: Lacking Documentation on Custom Related Fields Pre-Save Override
-------------------------------------+-------------------------------------
Reporter: s.shanabrook@… | Owner: anonymous
Type: | Status: assigned
Cleanup/optimization | Version: 1.4
Component: Documentation | Resolution:
Severity: Normal | Triage Stage: Accepted
Keywords: | Needs documentation: 1
Has patch: 0 | Patch needs improvement: 0
Needs tests: 0 | UI/UX: 0
Easy pickings: 1 |
-------------------------------------+-------------------------------------
Changes (by anonymous):

* owner: vijay_shanker => anonymous


--
Ticket URL: <https://code.djangoproject.com/ticket/19427#comment:4>

Django

unread,
Jul 4, 2013, 9:57:18 AM7/4/13
to django-...@googlegroups.com
#19427: Lacking Documentation on Custom Related Fields Pre-Save Override
--------------------------------------+------------------------------------
Reporter: s.shanabrook@… | Owner:
Type: Cleanup/optimization | Status: new
Component: Documentation | Version: 1.4

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 1
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 1 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by timo):

* owner: anonymous =>
* status: assigned => new


--
Ticket URL: <https://code.djangoproject.com/ticket/19427#comment:5>

Django

unread,
Sep 18, 2013, 2:10:24 PM9/18/13
to django-...@googlegroups.com
#19427: Lacking Documentation on Custom Related Fields Pre-Save Override
--------------------------------------+------------------------------------
Reporter: s.shanabrook@… | Owner:
Type: Cleanup/optimization | Status: new
Component: Documentation | Version: 1.4

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by timo):

* needs_docs: 1 => 0
* easy: 1 => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/19427#comment:6>

Django

unread,
Jul 24, 2015, 11:29:46 AM7/24/15
to django-...@googlegroups.com
#19427: Lacking Documentation on Custom Related Fields Pre-Save Override
--------------------------------------+------------------------------------
Reporter: s.shanabrook@… | Owner:
Type: Cleanup/optimization | Status: closed
Component: Documentation | Version: 1.4
Severity: Normal | Resolution: wontfix

Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by timgraham):

* status: new => closed
* resolution: => wontfix


Comment:

Not sure what to do here as `attname` isn't referenced in the linked doc
(maybe it's since been removed). This seems in the realm of private API
anyway.

--
Ticket URL: <https://code.djangoproject.com/ticket/19427#comment:7>

Reply all
Reply to author
Forward
0 new messages