Opened 4 years ago

Closed 4 years ago

#32868 closed New feature (wontfix)

Add system check or warning for model fields shadowing methods / other attributes

Reported by: Adam Johnson Owned by: nobody
Component: Core (System checks) Version: dev
Severity: Normal Keywords:
Cc: Carlton Gibson Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

It's possible to create a field name that replaces a Model method, for example:

from django.db import models


class Book(models.Model):
    delete = models.BooleanField(default=False)

Instances of this model then cannot be deleted with instance.delete(). (They can be with Model.delete(instance), but that's a really rare way of using a method so I expect most developers would not think of that.)

Django could warn about this, at least for some key names that sound like feasible field names, e.g. delete, save, pk, clean, ...

It's already not possible to create a field called check since the system check models.E020 warns if the Model.check() method has been replaced, added in #23615. We could see this ticket as an extension of that, "reserving" more names that Django may depend on.

Change History (1)

comment:1 by Mariusz Felisiak, 4 years ago

Cc: Carlton Gibson added
Resolution: wontfix
Status: newclosed

Thanks for this proposition, however I'm skeptical. I don't think it's worth additional complexity and potential backward incompatibility. In most of cases Django will already crash, e.g.

>>> obj.save()
TypeError: 'bool' object is not callable

>>> obj.delete()
TypeError: 'bool' object is not callable

The extra check added in #23615 was already a bit controversial (see discussion). You can raise the idea on the DevelopersMailingList to reach a wider audience and see what other think.

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