Opened 6 years ago
Last modified 6 years ago
#29691 closed New feature
Support ForeignKey based model inheritance — at Version 1
Reported by: | James Pic | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 2.1 |
Severity: | Normal | Keywords: | |
Cc: | James Pic | Triage Stage: | Unreviewed |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description (last modified by )
Currently, a model inheriting another will have a 1:1 relation with an automatically managed OneToOneField. This means a parent model instance may only have one child model instance. However, sometimes it may be interresting to have several child model instances for one parent model instance.
For example, creating a new child model with the same parent results in an error like:
django.db.utils.IntegrityError: UNIQUE constraint failed: djcall_call.callable_ptr_id
But overriding the parent field with a ForeignKey is not accepted:
django.core.exceptions.FieldError: Auto-generated field 'callable_ptr' in class 'Call' for parent_link to base class 'Callable' clashes with declared field of the same name.
Can we perhaps add an exception (not Exception!) for child models that override the automatic ptr field that is a OneToOneField, with a ForeignKey ?
An example use case:
class Caller(models.Model): callback = models.CharField() max_attemps = models.IntegerField(default=1) def call(self): call = Call(caller_ptr=self) try: call.execute() except: if self.max_attempts > Call.objects.filter(caller_ptr=self): return self.call() raise return call.result class Call(Caller): result = models.PickleField() def call(self): self.result = import_string(self.callback)() return self.result
Thanks