For example we have such model:
{{{
#!python
class ModelWithContentTypeField(models.Model):
ct = ContentTypeField(on_delete=models.CASCADE)
}}}
In comparison with `ForeignKey` `ContentTypeField` will have these
features:
1. `ModelWithContentTypeField.objects.first().ct` will use content types
cache.
2. `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]])`
3. Creation using a model class as a value:
`ModelWithContentTypeField.objects.create(ct=FooModel)`
Here's [https://github.com/sir-sigurd/django/tree/contenttype-field-3
rough implementation].
--
Ticket URL: <https://code.djangoproject.com/ticket/27580>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* owner: nobody => Sergey Fedoseev
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/27580#comment:1>
Comment (by Jani Tiainen):
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.
--
Ticket URL: <https://code.djangoproject.com/ticket/27580#comment:2>
* stage: Unreviewed => Someday/Maybe
Comment:
Sergey, could you write to the DevelopersMailingList to get feedback on
the idea?
--
Ticket URL: <https://code.djangoproject.com/ticket/27580#comment:3>
* cc: me@… (added)
--
Ticket URL: <https://code.djangoproject.com/ticket/27580#comment:4>
Comment (by Marten Kenbeek):
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.
--
Ticket URL: <https://code.djangoproject.com/ticket/27580#comment:5>
Comment (by Sergey Fedoseev):
Replying to [comment:5 Marten Kenbeek]:
> 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.
Thanks for the hint!
--
Ticket URL: <https://code.djangoproject.com/ticket/27580#comment:6>
* owner: Sergey Fedoseev => (none)
* status: assigned => new
--
Ticket URL: <https://code.djangoproject.com/ticket/27580#comment:7>