Opened 7 years ago
Closed 7 years ago
#28867 closed Cleanup/optimization (fixed)
Add a system check for a model property name that clashes with a foreign key id accessor
Reported by: | Michael | Owned by: | shangdahao |
---|---|---|---|
Component: | Core (System checks) | Version: | 1.11 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Accepted | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description (last modified by )
Summary
Given a model with a foreign key field "foo", it would be helpful if Django warned users when they include a property in the model named "foo_id" in the same model.
Steps To Reproduce
- Implement a relationship that looks like Exhibit A
- Create an instance of
Bar
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(models.Model): foo = models.ForeignKey(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
Change History (6)
comment:1 by , 7 years ago
Description: | modified (diff) |
---|
comment:2 by , 7 years ago
Component: | Uncategorized → Core (System checks) |
---|---|
Summary: | UX: Unhelpful AttributeError when a model property name ends with a `_id` → Add a system check for a model property name that clashes with a foreign key id accessor |
Triage Stage: | Unreviewed → Accepted |
Type: | Bug → Cleanup/optimization |
UI/UX: | unset |
comment:3 by , 7 years ago
Description: | modified (diff) |
---|
comment:4 by , 7 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:5 by , 7 years ago
Has patch: | set |
---|
Note:
See TracTickets
for help on using tickets.
PR