1

I'm trying to develop a server-side API for my Django project, but I've came up with a whole bunch of methods defined in every model declaration in the models.py files. So I thought to create a sort of Façade classes to handle the API method, leaving the models only with their properties.
I used to have this structure:

# models.py

class MyModel(models.Model):
    prop1 = ...
    prop2 = ...

    def f1(self):
        pass
    def f2(self):
        pass

While now I I have this one:

# models.py

class MyModel(models.Model):
    prop1 = ...
    prop2 = ...

# wrappers.py

from myapp import models


class MyModel(models.MyModel):

    def f1(self):
        pass
    def f2(self):
        pass

This was pretty interesting while I was writing it down, because it allowed me to separate definition of functionality without losing none of them. However, when I tried to test the new data model, I got very confused.

$ python manage.py shell
Python 2.7.3 (default, Apr 20 2012, 22:44:07) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from myapp.wrappers import MyModel
>>> MyModel
<class 'MyProject.myapp.models.MyModel'>

WTF is going on here!? Why do I get the model class when I'm asking for the "wrapper" one instead?
Of course, this keeps all the properties of the model but obviously lacks all of the methods defined in the wrapper.
I hope you can help me with this because it's a completely nonsense for me. Thank you for your help.

cronos2
  • 288
  • 5
  • 13

1 Answers1

2

A little further down in the documentation is "proxy models". Those will do as you want.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358