Opened 8 years ago
Last modified 6 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 |
Pull Requests: | How to create a pull request | ||
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.
According to the ticket's flags, the next step(s) to move this issue forward are:
Unknown. The Someday/Maybe triage stage is used to keep track of high-level ideas or long term feature requests.
It could be an issue that's blocked until a future version of Django (if so, Keywords will contain that version number). It could also be an enhancement request that we might consider adding someday to the framework if an excellent patch is submitted.
If you're interested in contributing to the issue, raising your ideas on the Django Forum would be a great place to start.
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 , 6 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.