views:

80

answers:

2

How to model this in django:

1) have a base network of manufacturers

2) under each network their might be several distributors

3) a user of the system can access items through the distributor

4) if a user access the item through the distributor we want that item to be translated where each manufacturer will have their own translation

class Manufacturer(models.Model):
    networkname = models.CharField(max_length=128)

    class Meta:
        proxy = True

class Distributor(models.Model):
    man = models.ForeignKey(Manufacturer)

class ManuType1(Manufacturer):
    def translate(self, str):
        return 'translate'

class ManuType2(Manufacturer):
    def translate(self, str):
        return 'translate'

In this scenario we will get a request for a certain Distributor. We identify that distributor and we want to call that distributors manufacturers translate method. Does this look like a way to model this in django (I'm sure there are many ways to do this) so any input/feedback is useful.

Where I run into problems (not knowing python well enough perhaps) is given a Distributor with ManuType1 How do I call the translate function at runtime?

This is probably a well explored pattern using other terms, just not sure how to express it exactly.

A: 

If dist is an instance of Distributor, then you can do dist.man to get the Manufacturer instance. Due to the way multi-table inheritance works in Django, you'll need to access the OneToOneField that exists on the Manufacturer to the subclass instance. The problem lies in figuring out which subclass instance exists. This can be made easier by storing the ContentType of the subclass in the Manufacturer instance.

Joseph Spiros
Thanks for your answer, following that method seems what I'm basically looking for, but what about if I want to be able to add a specific ManuType in the admin without having my admin get cluttered with all of the different possible manufacturers?In other words, I would like my admin have the ability to create a new Manufacturer and select the specific instance (ManuType1, ManuType2...) when they attempt to create that manufacturer type.Then when a user creates a new Distributor they would be able to pick from all the different manufacturers to associate the distributor with.....?
jmat
That may be possible with a custom ModelAdmin and perhaps a custom Form instance that does all the work of providing the appropriate form and then creating the appropriate instance as a result, instead of just a plain Manufacturer instance. Unfortunately, Django does not currently support a way of adding a subclass instance to a superclass instance after the superclass instance already exists; you have to create the subclass instance in the first place. And, also unfortunately, Django's admin does not play as well with multi-table inheritance as it does with everything else.
Joseph Spiros
Thanks again for you prompt answers...Any idea how you could select the instances to display? That part has me a bit confused, I have seen examples of the custom Form/Admin instances around...
jmat
A: 

Joseph, your answer worked. I was able to use the class name as well to select the appropriate Manufacturer when a distributor is selected.

jmat