Opened 8 years ago

Closed 7 years ago

Last modified 7 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 Changed 8 years ago by Baylee Feore

Owner: changed from nobody to Baylee Feore
Status: newassigned

comment:2 Changed 8 years ago by Tim Graham

Triage Stage: UnreviewedAccepted

comment:3 Changed 8 years ago by Phil Krylov

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 Changed 8 years ago by Tim Graham

Has patch: set
Patch needs improvement: set

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

comment:5 Changed 7 years ago by Baylee Feore

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

comment:6 Changed 7 years ago by Tim Graham

Patch needs improvement: unset

comment:7 Changed 7 years ago by Philip James

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 Changed 7 years ago by Tim Graham <timograham@…>

Resolution: fixed
Status: assignedclosed

In 7767978b:

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

comment:9 Changed 7 years ago by Tim Graham <timograham@…>

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 Changed 7 years ago by Tim Graham <timograham@…>

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