Opened 5 years ago

Closed 5 years ago

#21711 closed Bug (fixed)

Check for duplicate model names

Reported by: Aymeric Augustin Owned by: nobody
Component: Core (Other) Version: master
Severity: Normal Keywords: app-loading
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


(Extracted from #21681.)

ModelBase.__new__ has an interesting behavior: it checks for a model class with the target name in the app registry and return it instead of creating a new class if found.

# myapp/

from django.db import models

class MyModel(models.Model):

first_model_class = MyModel  # save a reference

class MyModel(models.Model):

second_model_class = MyModel  # save a reference

if second_model_class is first_model_class:
    raise RuntimeError("WTF")

It would be better to raise an error in that case than silently return the first model. See also #21679 which is essentially the same issue for applications.

Change History (5)

comment:1 Changed 5 years ago by Aymeric Augustin

This will likely be a problem for deferred queries.

comment:2 Changed 5 years ago by Anssi Kääriäinen

We could cache the deferred classes in deferred_class_factory.

comment:3 Changed 5 years ago by Aymeric Augustin

Yes yes yes. In fact I'd like to avoid storing these models in the app registry entirely.

comment:4 Changed 5 years ago by Aymeric Augustin

This will also prevent importing twice the same models through different paths.

comment:5 Changed 5 years ago by Aymeric Augustin <aymeric.augustin@…>

Resolution: fixed
Status: newclosed

In f630373b929bc62bf4d66d60c532f7832e5fbe67:

Fixed #21711 -- Enforced unicity of model names.

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