Code

Opened 19 months ago

Closed 12 months ago

Last modified 12 months 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 timo 12 months ago.

Download all attachments as: .zip

Change History (9)

comment:1 Changed 19 months ago by aaugustin

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

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 19 months ago by aaugustin

  • Triage Stage changed from Unreviewed to Design decision needed
  • Type changed from Uncategorized to New feature

comment:3 Changed 16 months ago by aaugustin

  • Triage Stage changed from Design decision needed to Accepted

We'll have to fix this at some point.

Changed 12 months ago by timo

comment:4 Changed 12 months ago by timo

  • Has patch set

comment:5 Changed 12 months ago by claudep

  • Triage Stage changed from Accepted to Ready for checkin
  • Type changed from New feature to Cleanup/optimization

comment:6 Changed 12 months ago by Tim Graham <timograham@…>

  • Resolution set to fixed
  • Status changed from new to closed

In b9fceadfd440daec09dee3c7c9d01997f94ec94f:

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

Thanks astorije@ for the report.

comment:7 Changed 12 months 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 12 months 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.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.