Opened 8 years ago

Closed 8 years ago

Last modified 8 years ago

#26702 closed Cleanup/optimization (fixed)

Document custom field subclass change requires new custom field class

Reported by: Baylee Feore Owned by: Baylee Feore
Component: Documentation Version: 1.9
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Related to #26586

Can't alter a custom field to have a different base field type. Documentation should be updated to reflect this and proposed solution.

Example before state:

class CustomCharField(models.CharField):
    def foo(self):
        pass

class Author(models.Model):
    name = CustomCharField(max_length=255)

Example of what won't work:

class CustomCharField(models.TextField):
    def foo(self):
        pass

class Author(models.Model):
    name = CustomCharField(max_length=255)

Example of what you should change to:

class CustomFieldMixin(object):
    def foo(self):
        pass

class CustomCharField(CustomFieldMixin, models.CharField):
    pass

class CustomTextField(CustomFieldMixin, models.TextField):
    pass

class Author(models.Model):
    name = CustomTextField(max_length=255)

Should also include link to: https://docs.djangoproject.com/en/1.9/topics/migrations/#considerations-when-removing-model-fields to explain why user must keep old field definition around.

Change History (10)

comment:1 by Baylee Feore, 8 years ago

Owner: changed from nobody to Baylee Feore
Status: newassigned

comment:2 by Tim Graham, 8 years ago

Triage Stage: UnreviewedAccepted

comment:3 by Phil Krylov, 8 years ago

The method/mixin part is confusing for an average reader who is searching for a simple case; I think that it should be an example separate from the original case in #26586 where no mixin is needed.

comment:4 by Tim Graham, 8 years ago

Has patch: set
Patch needs improvement: set

PR. I haven't reviewed it myself, but marking as needs improvement per previous comment.

comment:5 by Baylee Feore, 8 years ago

That makes sense. I've updated the PR to use a simplified example.

comment:6 by Tim Graham, 8 years ago

Patch needs improvement: unset

comment:7 by Philip James, 8 years ago

Triage Stage: AcceptedReady for checkin

This doc patch is clear and concise, doesn't cover anything extraneous, and seems to meet all the docs guidelines.

comment:8 by Tim Graham <timograham@…>, 8 years ago

Resolution: fixed
Status: assignedclosed

In 7767978b:

Fixed #26702 -- Documented how to change the base class of a custom field.

comment:9 by Tim Graham <timograham@…>, 8 years ago

In 4765769b:

[1.9.x] Fixed #26702 -- Documented how to change the base class of a custom field.

Backport of 7767978beec6098baea75d50a191a3b8224e729f from master

comment:10 by Tim Graham <timograham@…>, 8 years ago

In 5e46075:

[1.10.x] Fixed #26702 -- Documented how to change the base class of a custom field.

Backport of 7767978beec6098baea75d50a191a3b8224e729f from master

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