#29691: Support ForeignKey based model inheritance
-------------------------------------+-------------------------------------
Reporter: James Pic | Owner: nobody
Type: New feature | Status: new
Component: Database layer | Version: 2.1
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by James Pic:
Old description:
> 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 ?
>
> Thanks
New description:
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
--
--
Ticket URL: <https://code.djangoproject.com/ticket/29691#comment:1>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/062.4c1b471bd8c91c9073c5c79f9cb1ff76%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.