#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 shangdahao)


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):

class Bar(models.Model):
  foo = models.ForeignKey(Foo)

  def foo_id(self):
    return self.foo.id
// Exhibit B
Traceback (most recent call last):
  File "./manage.py", line 22, in <module>
  File "/usr/local/lib/python3.6/dist-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
  File "/usr/local/lib/python3.6/dist-packages/django/core/management/__init__.py", line 356, in execute
  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
  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
  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 Changed 11 months ago by Michael

Description: modified (diff)

comment:2 Changed 11 months ago by Tim Graham

Component: UncategorizedCore (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: UnreviewedAccepted
Type: BugCleanup/optimization
UI/UX: unset

comment:3 Changed 10 months ago by shangdahao

Description: modified (diff)

comment:4 Changed 10 months ago by shangdahao

Owner: changed from nobody to shangdahao
Status: newassigned

comment:5 Changed 10 months ago by shangdahao

Has patch: set

comment:6 Changed 10 months ago by Tim Graham <timograham@…>

Resolution: fixed
Status: assignedclosed

In cc6bcc6:

Fixed #28867 -- Added system check for a model property that clashes with a related field accessor.

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