Opened 13 years ago

Closed 12 years ago

Last modified 12 years ago

#17436 closed Cleanup/optimization (fixed)

flag attempts to change signature of __init__ on Models

Reported by: straz Owned by: Zbigniew Siciarz
Component: Documentation Version: 1.3
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

As a relative Django newbie, I tried to redefine the init method on my Model classes to take additional parameters.
This failed silently - the objects were created but Model.save() did not persist the objects to the database.
I didn't see anything in the Models documentation or FAQ about this, either.

After some searching I found the answer here, namely, "don't do that":
http://stackoverflow.com/questions/843580/writing-a-init-function-to-be-used-in-django-model

Feature request: add a warning which is thrown whenever you subclass django.db.models.Model
with a nonstandard init method. The wording could be something like: "Changing the calling signature
of init on subclasses of Model may prevent objects from being saved correctly. Try using a
classmethod factory pattern to create objects instead."

Doc request: add this to the Models documentation

Attachments (1)

patch_17436.diff (1.6 KB ) - added by Zbigniew Siciarz 13 years ago.
Note about overriding init and other approaches to customize instance creation.

Download all attachments as: .zip

Change History (6)

comment:1 by Aymeric Augustin, 13 years ago

Component: Database layer (models, ORM)Documentation
Triage Stage: UnreviewedAccepted
Type: New featureCleanup/optimization

Accepted as a documentation request.

With a dynamic language, it will always be possible for a developers to shoot himself into the foot. We can't put warnings for all possible mistakes.

comment:2 by Zbigniew Siciarz, 13 years ago

Owner: changed from nobody to Zbigniew Siciarz

by Zbigniew Siciarz, 13 years ago

Attachment: patch_17436.diff added

Note about overriding init and other approaches to customize instance creation.

comment:3 by Zbigniew Siciarz, 13 years ago

Has patch: set

comment:4 by Tim Graham <timgraham@…>, 12 years ago

Resolution: fixed
Status: newclosed

In [7313468f85c3ade1275fc7fef193a4429f4af4bf]:

Fixed #17436 - Added warning about overriding Model.init()

Thanks zsiciarz for the draft patch.

comment:5 by Tim Graham <timgraham@…>, 12 years ago

In [c5e35afbccb2777b8eea882e95eba44ed582c361]:

[1.4.X] Fixed #17436 - Added warning about overriding Model.init()

Thanks zsiciarz for the draft patch.

Backport of 7313468f85 from master

Note: See TracTickets for help on using tickets.
Back to Top