`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/models.py
from django.db import models
class MyModel(models.Model):
pass
first_model_class = MyModel # save a reference
class MyModel(models.Model):
pass
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.
--
Ticket URL: <https://code.djangoproject.com/ticket/21711>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
Comment (by aaugustin):
This will likely be a problem for deferred queries.
--
Ticket URL: <https://code.djangoproject.com/ticket/21711#comment:1>
Comment (by akaariai):
We could cache the deferred classes in deferred_class_factory.
--
Ticket URL: <https://code.djangoproject.com/ticket/21711#comment:2>
Comment (by aaugustin):
Yes yes yes. In fact I'd like to avoid storing these models in the app
registry entirely.
--
Ticket URL: <https://code.djangoproject.com/ticket/21711#comment:3>
Comment (by aaugustin):
This will also prevent importing twice the same models through different
paths.
--
Ticket URL: <https://code.djangoproject.com/ticket/21711#comment:4>
* status: new => closed
* resolution: => fixed
Comment:
In [changeset:"f630373b929bc62bf4d66d60c532f7832e5fbe67"]:
{{{
#!CommitTicketReference repository=""
revision="f630373b929bc62bf4d66d60c532f7832e5fbe67"
Fixed #21711 -- Enforced unicity of model names.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/21711#comment:5>