Maybe I'm getting closer, maybe not.  What I did was create a bunch of
different subclasses of ServiceRequest, then created a bunch of
inlines within the admin interface for each type.  The admin user has
to be smart enough to pick the right kind on their own, but that's OK.
I did it like this:

# in my models
class ServiceRequest(models.Model):
    class Meta:
        abstract = True
    customer = models.ForeignKey(Customer)

class NormalRequest(ServiceRequest):
    def notify(self):
        # set database flag

class ImportantRequest(ServiceRequest):
    def notify(self):
       # send email

class CriticalRequest(ServiceRequest):
    def notify(self):
        # send SMS

# in my admin.py
class NormalInline(admin.TabularInline):
    model = NormalRequest
    extra = 1

class ImportantInline(admin.TabularInline):
    model = ImportantRequest
    extra = 1

class CriticalInline(admin.TabularInline):
    model = CriticalRequest
    extra = 1

class CustomerAdmin(admin.ModelAdmin):
    fields = ['name','phone']
    inlines = [NormalInline, ImportantInline, CriticalInline]

So far, so good.  But I apparently have my inheritance setup wrong.
When my Customer class tries to get it's requests, I get a series of
ServiceRequest items, NOT the concrete classes that I want.  If I try
to cycle through all the types ("for sr_set in
[customer.normalrequest_set, customer.importantrequest_set,
customer.criticalrequest_set]"), the system complains about "unknown
column 'id' in 'field list'").  If I use proxy classes, then all the
service requests get duped in the admin interface.  And something else
went wrong if I use concrete inheritance. (Sorry, I can't remember
what it was.)

Has anyone else done a pattern like this?  (One-to-Many, where the
"Many" side can be a series of subclasses?)


On Dec 23, 8:58 am, Chris Curvey <ccur...@gmail.com> wrote:
> this may be beyond the current abilities of the Django auto-generated
> admin interface, but I thought I'd ask...
>
> I have a class called "Customer" which has a one-to-many relationship
> with "ServiceRequest".  I've got that all working through the admin
> interface and it's working fine, like this:
>
> class ServiceRequestInline(admin.TabularInline):
>     model = ServiceRequest
>     extra = 3
>
> class CustomerAdmin(admin.ModelAdmin):
>     fields = ['name','phone']
>     inlines = [ServiceRequestInline]
>
> Now I want to have different subclasses of ServiceRequest.  All my
> ServiceRequests have the same attributes and methods, but the
> implementations of the methods differ.  (For example, the
> "notify_team" method might set a flag in a database, or send an email,
> or send an SMS, depending on the service type.)
>
> Is there a way to tell the admin interface that I want the customer
> page to allow the user to pick a subclass of service requests?

--

You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.


Reply via email to