Opened 6 years ago

Closed 6 years ago

Last modified 6 years ago

#29691 closed New feature (wontfix)

Support ForeignKey based model inheritance

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 James Pic)

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

Change History (4)

comment:1 by James Pic, 6 years ago

Description: modified (diff)

comment:2 by Tim Graham, 6 years ago

I have a feeling that breaking the one-on-one link between parent and child in model inheritance would add a lot of complexity and bugs. I think using a ForeignKey without inheritance would be the way to go.

comment:3 by Carlton Gibson, 6 years ago

Resolution: wontfix
Status: newclosed

I strongly suspect that Tim's initial judgement here is correct.

Even if it's not though, changing the way model inheritance works would be such a big change that I think it would need to go through the whole DEP process, rather than be something we could just add as a normal New Feature.

As such, I'm going to close this here. The following step (if any) would be "Pre-proposal".

comment:4 by James Pic, 6 years ago

Thanks a heap for your feedback,

I'll try the metaclass override, there's a large method to override, if it works for this use case i will make a DEP then (to avoid maintaining the pochack accross stable django releases)

Have a great day

Last edited 6 years ago by James Pic (previous) (diff)
Note: See TracTickets for help on using tickets.
Back to Top