Opened 4 years ago

Closed 3 years ago

Last modified 3 years ago

#19539 closed Cleanup/optimization (fixed)

Writing custom model fields: The __metaclass__ attribute is no longer supported in Python 3

Reported by: astorije@… Owned by: nobody
Component: Documentation Version: 1.5-alpha-1
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

As Django 1.5 introduces the support of Python 3, I think the documentation of "Writing custom model fields" should warn developers that:

class MyCustomField(models.Field):
    __metaclass__ = models.SubfieldBase

is no longer supported and that they should use:

class MyCustomField(models.Field, metaclass=models.SubfieldBase):
    ...

instead.

Writing in the first (old) fashion results in no errors or warnings whatsoever. The to_python() method is just not called as it should be...

Attachments (1)

19539.diff (1.2 KB) - added by Tim Graham 3 years ago.

Download all attachments as: .zip

Change History (9)

comment:1 Changed 4 years ago by Aymeric Augustin

The second version isn't acceptable as is because it's a syntax error under Python 2. If you want a single snippet that works in Python 2 and 3 you have to use six.with_metaclass.

Currently the documentation assumes Python 2. We haven't chosen how to document things that are different in Python 2 and 3 yet.

Technically, this ticket is valid, but it covers about 0.1% of the problem. For starters, the docs has tons of __unicode__ methods that don't make any sense under Python 3...

comment:2 Changed 4 years ago by Aymeric Augustin

Triage Stage: UnreviewedDesign decision needed
Type: UncategorizedNew feature

comment:3 Changed 4 years ago by Aymeric Augustin

Triage Stage: Design decision neededAccepted

We'll have to fix this at some point.

Changed 3 years ago by Tim Graham

Attachment: 19539.diff added

comment:4 Changed 3 years ago by Tim Graham

Has patch: set

comment:5 Changed 3 years ago by Claude Paroz

Triage Stage: AcceptedReady for checkin
Type: New featureCleanup/optimization

comment:6 Changed 3 years ago by Tim Graham <timograham@…>

Resolution: fixed
Status: newclosed

In b9fceadfd440daec09dee3c7c9d01997f94ec94f:

Fixed #19539 -- Updated custom model fields example for Python 3.

Thanks astorije@ for the report.

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

In 6c984587711f82c0fc5223bbf6e8bb3b71732a3e:

[1.5.x] Fixed #19539 -- Updated custom model fields example for Python 3.

Thanks astorije@ for the report.

Backport of b9fceadfd4 from master.

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

In 14a5b79e294506ab045a14fbec89030510f1cefe:

[1.6.x] Fixed #19539 -- Updated custom model fields example for Python 3.

Thanks astorije@ for the report.

Backport of b9fceadfd4 from master.

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