Django method structure that cover different models

I have two models (like A and B) that are independent and have independent methods. I want to add some methods that work on both models.

For example addX () will create an object from both models A and B.

What's the best way to structure your code in this situation? It doesn't make sense that the method belongs to any of the model methods. Is there a standard for writing services for this kind of "abstract" model?

+2


a source to share


1 answer


I'm not sure I fully understand your question. Are you asking where to put generic methods, or are you asking how to call one method to work on two classes?

If you just need to have common methods, then I'll have an abstract parent model where both subclasses of the model are:

class ParentModel(models.Model):

    class Meta:
        abstract = True

    def some_shared_method(self):
        ...

class A(ParentModel):
    ...

class B(ParentModel):
    ...

      



the abstract meta parameter tells Django not to create any actual db tables for the ParentModel. It's just for storing methods.

Check it out for more information: http://docs.djangoproject.com/en/dev/topics/db/models/#id6

0


a source







All Articles