Opened 9 years ago

Closed 9 years ago

Last modified 9 years ago

#25016 closed Bug (fixed)

The related_name of ForeignKey cannot be Unicode name after Django 1.8

Reported by: A-hông Owned by: nobody
Component: Database layer (models, ORM) Version: 1.8
Severity: Release blocker Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description

I asked in django-user

This code can run before django 1.7
But "related_name" got an error after django 1.8

使用者表.來源: (fields.E306) The name '使用者' is invalid related_name for field 使用者表.來源
class 來源表(models.Model):
    名 = models.CharField(max_length=100)

class 使用者表(models.Model):
    來源 = models.OneToOneField(來源表, related_name='使用者', primary_key=True, null=False)

Tim Graham said "It looks like the check added in ticket #22064 may be too strict (only allowing alphanumeric characters in related_name)."

Change History (10)

comment:1 by A-hông, 9 years ago

I think using

re.match('^[^\W\d]\w*$', related_name)

instead of

re.match('^[a-zA-Z_][a-zA-Z0-9_]*$', related_name)

in django/db/models/fields/related.py

comment:2 by Simon Charette, 9 years ago

Triage Stage: UnreviewedAccepted
Type: UncategorizedBug

I think we should default to the old behavior on Py2: re.match('^[^\W\d]\w*$', related_name, re.unicode if six.PY3 else 0)

While we're at it we should also replace the trailing $ by a \Z to prevent 'related_name\n' from being accepted.

comment:3 by A-hông, 9 years ago

I add some tests and do these in https://github.com/django/django/pull/4911

Last edited 9 years ago by A-hông (previous) (diff)

comment:4 by Baptiste Mispelon, 9 years ago

In python 3, we should probably use str.isidentifier [1] instead of coding an ad-hoc regex.

[1] https://docs.python.org/3/library/stdtypes.html?highlight=str.isidentifier#str.isidentifier

comment:5 by A-hông, 9 years ago

How to write Unicode test like '試驗' string in python2 ?

related_name does not support unicode string in python2

comment:6 by zauddelig, 9 years ago

I added some notes on how make the tests work on github.

comment:7 by Tim Graham, 9 years ago

Has patch: set
Patch needs improvement: set
Severity: NormalRelease blocker

comment:8 by A-hông, 9 years ago

Thank for your suggestions. I updated the PR in github..

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

Resolution: fixed
Status: newclosed

In d3e12c9:

Fixed #25016 -- Reallowed non-ASCII values for ForeignKey.related_name on Python 3.

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

In a97e50c5:

[1.8.x] Fixed #25016 -- Reallowed non-ASCII values for ForeignKey.related_name on Python 3.

Backport of d3e12c901777697b7bf08b25e2dd46f0b951db8c from master

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