#21414 closed Cleanup/optimization (fixed)
Remove django.db.models.related.RelatedObject
Reported by: | Anssi Kääriäinen | Owned by: | Tim Graham |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | loic@… | Triage Stage: | Accepted |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Currently related fields have two similar attributes, self.rel and self.related. The first is ForeignObjectRel subclass, the second is RelatedObject. Both of these do almost the same thing, and it doesn't seem necessary to have both rel and related. It is confusing to try to remember which one does what.
In the proposed patch at https://github.com/akaariai/django/tree/related_rel_removal the RelatedObject usage is removed. The idea is to make ForeignObjectRel to work exactly like RelatedObject worked and provide the same instance from field.rel and field.related.
I've opted for deprecation path where RelatedObject can still be used, and so can field.related, too. The field.related attribute is actually ForeignObjectRel, but by usage of __instancecheck__
isinstance(field.related, RelatedObject) answers yes. This should make this change easy for 3rd party apps.
Change History (12)
comment:1 by , 11 years ago
Cc: | added |
---|---|
Has patch: | set |
Needs documentation: | set |
Patch needs improvement: | set |
Triage Stage: | Unreviewed → Accepted |
Version: | 1.6 → master |
comment:3 by , 11 years ago
By docs I meant docs/internals/deprecation.txt
, dunno if this document is meant for public consumption or for internal housekeeping.
comment:4 by , 11 years ago
It's only for public APIs.
If we know that a private API is used in the wild, we might mention it there, but that's an exception.
comment:5 by , 11 years ago
Needs documentation: | unset |
---|
All good, I wrongly assumed it was a TODO list for when the new dev branch is created.
So yes, it's not needed here since this is very much private APIs.
comment:6 by , 10 years ago
Anssi, do you think there is much work to do to incorporate this in 1.8? Might it simply the _meta
refactor or should we try to merge that branch first?
comment:7 by , 10 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
I'm trying to update this to apply cleanly.
comment:9 by , 10 years ago
Resolution: | → fixed |
---|---|
Status: | assigned → closed |
comment:10 by , 10 years ago
Found two apps failing as they are using RelatedObject, django-taggit and django-import-export.
To fix, would I be right in thinking it is a case of replacing
from django.db.models.fields.related import ForeignObjectRel
with
from django.db.models.related import RelatedObject
and all instances of RelatedObject with ForeignObjectRel
?
Looking pretty good.
I left a couple of comments on commit 8d63a8e (linking to it since these are lost on rebase).
Dunno if it's an omission or still on the todo list, but it's missing the deprecation docs.