#16299 closed Bug (fixed)
Django model-module doesn't support unicode_literals
Reported by: | Owned by: | nobody | |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | dev |
Severity: | Normal | Keywords: | unicode, ForeignKey, AttributeError |
Cc: | Triage Stage: | Accepted | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | yes | UI/UX: | no |
Description
Hello all,
When using
from __future__ import unicode_literals
in a model file, Django fails to recognize a model referenced by a string and raises a quite obscure error message:
Traceback (most recent call last): File "test.py", line 5, in <module> a = article.Article.objects.get(id=57220255) File "/usr/local/lib/django/django/db/models/manager.py", line 132, in get return self.get_query_set().get(*args, **kwargs) File "/usr/local/lib/django/django/db/models/query.py", line 341, in get clone = self.filter(*args, **kwargs) File "/usr/local/lib/django/django/db/models/query.py", line 550, in filter return self._filter_or_exclude(False, *args, **kwargs) File "/usr/local/lib/django/django/db/models/query.py", line 568, in _filter_or_exclude clone.query.add_q(Q(*args, **kwargs)) File "/usr/local/lib/django/django/db/models/sql/query.py", line 1172, in add_q can_reuse=used_aliases, force_having=force_having) File "/usr/local/lib/django/django/db/models/sql/query.py", line 1060, in add_filter negate=negate, process_extras=process_extras) File "/usr/local/lib/django/django/db/models/sql/query.py", line 1226, in setup_joins field, model, direct, m2m = opts.get_field_by_name(name) File "/usr/local/lib/django/django/db/models/options.py", line 307, in get_field_by_name cache = self.init_name_map() File "/usr/local/lib/django/django/db/models/options.py", line 339, in init_name_map for f, model in self.get_all_related_objects_with_model(): File "/usr/local/lib/django/django/db/models/options.py", line 371, in get_all_related_objects_with_model self._fill_related_objects_cache() File "/usr/local/lib/django/django/db/models/options.py", line 393, in _fill_related_objects_cache if f.rel and not isinstance(f.rel.to, str) and self == f.rel.to._meta: AttributeError: 'unicode' object has no attribute '_meta'
Model example:
insert_user = models.ForeignKey("models.User", db_column='insertuser_id') # Or without from __future__ import unicode_literals insert_user = models.ForeignKey(u"models.User", db_column='insertuser_id')
I've attached a patch.
Yours,
Martijn
Attachments (4)
Change History (10)
by , 13 years ago
Attachment: | support-for-unicode-literals.diff added |
---|
comment:1 by , 13 years ago
Needs documentation: | set |
---|---|
Needs tests: | set |
Patch needs improvement: | set |
Triage Stage: | Unreviewed → Accepted |
Accepted, but shouldn't the check be isinstance(obj, basestring)? Also, the patch still needs docs and tests.
comment:2 by , 13 years ago
shouldn't the check be isinstance(obj, basestring)?
Yes, you're correct. Fixed!
needs docs
I've added documentation to the diff. Is it sufficient?
and tests
I'm not sure where to put the unittest. Could you help me?
comment:3 by , 13 years ago
Needs documentation: | unset |
---|---|
Needs tests: | unset |
Patch needs improvement: | unset |
I've attached a patch (django-unicodely-named-model-field-tests.diff) which fixes the unicode issue and adds a test, but only for foreign key names which will convert cleanly to ascii.
Names which have 'real' unicode characters in them will still fail, but these cause errors in validation.py which are much harder to catch and fix.
I've also spoken directly to Russell, and he claims that a documentation patch isn't necessary for this bug.
by , 13 years ago
Attachment: | django-unicodely-named-model-field-tests.diff added |
---|
by , 13 years ago
Attachment: | django-unicodely-named-model-field-tests-2.diff added |
---|
Updated test which doesn't cause Foreign Key errors
Patch