views:

132

answers:

0

Hi there, I was wondering whether it was possible to subclass a model used as the intermediate model in a M2M relation - and then display it through the usual ModelInline procedure... The code will explain it better (in models.py):

from django.db import models
from django.contrib import admin


#the many2many_through model first
class Participation(models.Model):  
    event = models.ForeignKey('Event')
    person = models.ForeignKey('Person')
    date = models.DateField()
    role = models.CharField(max_length=50, unique=True)    

    def __unicode__(self):
        return self.role

# now a simple subclass of participation, e.g., where the role is given by default
class Participation_to_a_wedding(Participation):  
    role = models.CharField(max_length=50, default="photographer",)  


# the inlines
class ParticipationInline(admin.TabularInline):
    model = Participation
    extra = 2

class ParticipationWeddingInline(admin.TabularInline):
    model = Participation_to_a_wedding
    extra = 2
    exclude = ['role']


# the main models
class Person(models.Model):
   name = models.CharField(max_length=50, unique=True)
   present_at = models.ManyToManyField('Event', through='Participation', related_name="present1")
   present_at2 = models.ManyToManyField('Event', through='Participation_to_a_wedding', related_name="present2")

   class Admin(admin.ModelAdmin):
       inlines = (ParticipationInline, ) #ParticipationWeddingInline

   def __unicode__(self):
     return self.name

class Event(models.Model):
    name = models.CharField(max_length=200) 

    def __unicode__(self):
     return self.name

Then in admin.py I register everything as usual:

from myapp.people_events.models import *

admin.site.register(Person, Person.Admin)
admin.site.register(Event, )

Well everything seems to work fine in the admin, but when you hit the save button it throws an error:

OperationalError at /admin/people_events/person/1/

(1054, "Unknown column 'people_events_participation_to_a_wedding.person_id' in 'where clause'")

The person_id field is not in the participation_to_a_wedding table, cause it's inherited from the participation table (and therefore it should be retrieved through the one2one relationship)... but django hasn't realized that this time. Am I getting something wrong? A workaround is simple - just don't use the inheritance in the participation_to_a_wedding model - but I wanted to hear more about this issue..