Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#25016 closed Bug (fixed)

The related_name of ForeignKey cannot be Unicode name after Django 1.8

Reported by: sih4sing5hong5 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 Changed 5 years ago by sih4sing5hong5

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 Changed 5 years ago by Simon Charette

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 Changed 5 years ago by sih4sing5hong5

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

Last edited 5 years ago by sih4sing5hong5 (previous) (diff)

comment:4 Changed 5 years ago by Baptiste Mispelon

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 Changed 5 years ago by sih4sing5hong5

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

related_name does not support unicode string in python2

comment:6 Changed 5 years ago by zauddelig

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

comment:7 Changed 5 years ago by Tim Graham

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

comment:8 Changed 5 years ago by sih4sing5hong5

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

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

Resolution: fixed
Status: newclosed

In d3e12c9:

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

comment:10 Changed 5 years ago by Tim Graham <timograham@…>

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