#28867: UX: Unhelpful AttributeError when a model property name ends with a
`_id`
-----------------------------------------+------------------------
Reporter: Michael | Owner: nobody
Type: Bug | Status: new
Component: Uncategorized | Version: 1.11
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 1 |
-----------------------------------------+------------------------
**Summary**
Given a model with an FK field "foo", it would be helpful if Django warned
users when they include a property in a model named "foo_id" in the same
model.
**Steps To Reproduce**
- Implement a model relationship that looks like Exhibit A
- Create an instance using .create or FactoryBoy.
- Observe the following stack trace that occurs in Exhibit B
** Expected Results**
As a Django user, I expect the framework to warn me with a helpful error
if I name a property method that doesn't play nice with Django's ORM.
{{{
// Exhibit A
class Foo(models.Model):
pass
Class Bar(model.Modal):
foo = models.FK(Foo)
@property
def foo_id(self):
return self.foo.id
}}}
{{{
// Exhibit B
Traceback (most recent call last):
File "./manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python3.6/dist-
packages/django/core/management/__init__.py", line 364, in
execute_from_command_line
utility.execute()
File "/usr/local/lib/python3.6/dist-
packages/django/core/management/__init__.py", line 356, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python3.6/dist-
packages/django/core/management/base.py", line 283, in run_from_argv
self.execute(*args, **cmd_options)
File "/usr/local/lib/python3.6/dist-
packages/django/core/management/base.py", line 330, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python3.6/dist-
packages/django/core/management/commands/shell.py", line 101, in handle
exec(sys.stdin.read())
File "<string>", line 1, in <module>
File "/usr/local/lib/python3.6/dist-packages/factory/base.py", line 46,
in __call__
return cls.create(**kwargs)
File "/usr/local/lib/python3.6/dist-packages/factory/base.py", line 568,
in create
return cls._generate(enums.CREATE_STRATEGY, kwargs)
File "/usr/local/lib/python3.6/dist-packages/factory/base.py", line 505,
in _generate
return step.build()
File "/usr/local/lib/python3.6/dist-packages/factory/builder.py", line
279, in build
kwargs=kwargs,
File "/usr/local/lib/python3.6/dist-packages/factory/base.py", line 314,
in instantiate
return self.factory._create(model, *args, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/factory/django.py", line
165, in _create
return manager.create(*args, **kwargs)
File "/usr/local/lib/python3.6/dist-
packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/django/db/models/query.py",
line 392, in create
obj = self.model(**kwargs)
File "/usr/local/lib/python3.6/dist-packages/django/db/models/base.py",
line 555, in __init__
_setattr(self, field.name, rel_obj)
File "/usr/local/lib/python3.6/dist-
packages/django/db/models/fields/related_descriptors.py", line 253, in
__set__
setattr(instance, lh_field.attname, getattr(value, rh_field.attname))
AttributeError: can't set attribute
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/28867>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/050.a47ca102abd7fcf0346ffcf82dc59d63%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.