Opened 2 weeks ago

Last modified 2 weeks ago

#28867 new Cleanup/optimization

Add a system check for a model property name that clashes with a foreign key id accessor

Reported by: Michael Owned by: nobody
Component: Core (System checks) Version: 1.11
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Michael)


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(model.Modal):
  foo = models.FK(Foo)

  def foo_id(self):
// Exhibit B
Traceback (most recent call last):
  File "./", line 22, in <module>
  File "/usr/local/lib/python3.6/dist-packages/django/core/management/", line 364, in execute_from_command_line
  File "/usr/local/lib/python3.6/dist-packages/django/core/management/", line 356, in execute
  File "/usr/local/lib/python3.6/dist-packages/django/core/management/", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.6/dist-packages/django/core/management/", line 330, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python3.6/dist-packages/django/core/management/commands/", line 101, in handle
  File "<string>", line 1, in <module>
  File "/usr/local/lib/python3.6/dist-packages/factory/", line 46, in __call__
    return cls.create(**kwargs)
  File "/usr/local/lib/python3.6/dist-packages/factory/", line 568, in create
    return cls._generate(enums.CREATE_STRATEGY, kwargs)
  File "/usr/local/lib/python3.6/dist-packages/factory/", line 505, in _generate
  File "/usr/local/lib/python3.6/dist-packages/factory/", line 279, in build
  File "/usr/local/lib/python3.6/dist-packages/factory/", line 314, in instantiate
    return self.factory._create(model, *args, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/factory/", line 165, in _create
    return manager.create(*args, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/django/db/models/", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/django/db/models/", line 392, in create
    obj = self.model(**kwargs)
  File "/usr/local/lib/python3.6/dist-packages/django/db/models/", line 555, in __init__
    _setattr(self,, rel_obj)
  File "/usr/local/lib/python3.6/dist-packages/django/db/models/fields/", line 253, in __set__
    setattr(instance, lh_field.attname, getattr(value, rh_field.attname))
AttributeError: can't set attribute

Change History (2)

comment:1 Changed 2 weeks ago by Michael

Description: modified (diff)

comment:2 Changed 2 weeks 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
Note: See TracTickets for help on using tickets.
Back to Top