Opened 8 years ago
Last modified 4 months ago
#27580 new New feature
add special field for storing content types
Reported by: | Sergey Fedoseev | Owned by: | |
---|---|---|---|
Component: | contrib.contenttypes | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | me@…, Sage Abdullah | Triage Stage: | Someday/Maybe |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
ContentType
model is quite specific, so we could add the subclass of ForeignKey field with some specific features.
For example we have such model:
class ModelWithContentTypeField(models.Model): ct = ContentTypeField(on_delete=models.CASCADE)
In comparison with ForeignKey
ContentTypeField
will have these features:
ModelWithContentTypeField.objects.first().ct
will use content types cache.
ContentTypeField
will support lookups on the model classes:
ModelWithContentTypeField.objects.filter(ct=FooModel)
vs ModelWithContentTypeField.objects.filter(ct=ContentType.objects.get_for_model(FooModel))
ModelWithContentTypeField.objects.filter(ct__in=[FooModel, BarModel])
vs
ModelWithContentTypeField.objects.filter(ct__in=[ContentType.objects.get_for_model(model) in [FooModel, BarModel]])
- Creation using a model class as a value:
ModelWithContentTypeField.objects.create(ct=FooModel)
Here's rough implementation.
Change History (8)
comment:1 by , 8 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:2 by , 8 years ago
comment:3 by , 8 years ago
Triage Stage: | Unreviewed → Someday/Maybe |
---|
Sergey, could you write to the DevelopersMailingList to get feedback on the idea?
comment:4 by , 8 years ago
Cc: | added |
---|
follow-up: 6 comment:5 by , 8 years ago
Note that you shouldn't hardcode the ContentType
class anywhere, or migrations will refer to the wrong model. Use self.related_model
in the field, and self.field.related_model
in the descriptor.
comment:6 by , 8 years ago
Replying to Marten Kenbeek:
Note that you shouldn't hardcode the
ContentType
class anywhere, or migrations will refer to the wrong model. Useself.related_model
in the field, andself.field.related_model
in the descriptor.
Thanks for the hint!
comment:7 by , 3 years ago
Owner: | removed |
---|---|
Status: | assigned → new |
comment:8 by , 4 months ago
Cc: | added |
---|
Does this feature require changes in Django core which prevents implementing this as an external app? I only saw quite small change in actual Django core.