How to model this in django (inherited model, where each inherited model has a unique method)
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):
开发者_Python百科 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.
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.
精彩评论