tags:

views:

170

answers:

2

So here's a question for a Django wizard. I am inheriting an object Manager something along the lines as follows:

class MyObjManager(models.Manager):

    def total_values(self, myobj_id):
                return 49

    def what_the_heck(self, poll_id):
                return 48

class TestMe():

        def what_the_heck(self, poll_id):
               return 47

Now if I create a model that uses this object manager as follows it doesn't seem to work.

class MyObj(models.Model):
    x = models.CharField(max_length=200)
    y = models.DateTimeField('date published')
    objects = MyObjManager()
    kb = MyObjManager()
    testMe = TestMe()

    def total_values(self):  
     #A return self.objects.total_values(1)
            #B return self.objects.yyy(1) 
            #C return self.kb.what_the_heck(1)
            #D return 50
            #E return self.testMe.what_the_heck(1)
            #F return self.objects.what_the_heck(1)

Now suppose that A,B,C,D,E are commented out one at a time respectively. Here's the results if I call myObj.total_values. ( where myObj is instance of MyObj) A) Nothing B) Nothing C) Nothing D) 50 E) 47 F) Nothing

I've used two object managers in this example, but the same thing happens if I only use one. Any insights into why I can't seem to access the methods in my overriden object manager?

A: 

Is it unclear whether or not PollManager is actually inheriting from MyObjManager. Do you mean to call your class PollManager?

EDIT: You could try creating a constructor for MyObjManager. It would call Manager's constructor.

def __init__(self):
    models.Manager.__init__(self)

See if that works.

AlbertoPL
Thanks, for that. I had refactored for the example and missed those references to PollManager. I have edited the question.
Daniel
The only thing I can think of is that you're not importing Manager correctly.
AlbertoPL
I can't tell how your imports look from your code, however this should be the only import you need: from django.db import models
AlbertoPL
Thanks Alberto. I tried your suggestion and it didn't seem to help. The imports are OK, and the rest of the website runs OK. It almost seems as if "objects = MyObjManager()" is only modifying a local copy of objects ... once I leave the scope its back to the regular Object Manager.
Daniel
+3  A: 

For A, B, and C, you cannot use self... that is a instance method, and self refers to that instance... which an instance doesn't have manager methods.

E and F work because, once again you are calling instance methods, of which you have properly declared the attributes.

For A, B, C, replace self with MyObj

class MyObj(models.Model):
    x = models.CharField(max_length=200)
    y = models.DateTimeField('date published')
    objects = MyObjManager()
    kb = MyObjManager()
    testMe = TestMe()

    def total_values(self):             
        #A return MyObj.objects.total_values(1)
        #B return MyObj.objects.yyy(1) 
        #C return MyObj.kb.what_the_heck(1)
        #D return 50
        #E return self.testMe.what_the_heck(1)
        #F return self.objects.what_the_heck(1)

I have a hard time thinking of a scenario where you would find an instance of the model, and then call a function on that to get other instances of that model, though... (I'm suggesting that your approach might be awkward or incorrect, stating your bigger scope problem might help you get a better answer.)

phillc
Thanks for the response. E) uses 'testMe' which is declared the same way as 'objects', and yet the syntax appears to work just fine (self.testMe.what_the_heck(1) returns 47). So I don't think that you're correct in saying that A,B,C can't use self unless there is something else I'm missing.
Daniel
I totally agree that bigger scope would clarify, but then it would likely just bog down this already bloated question :). I just tried to distill things in this admittedly contrived example.
Daniel
The magic comes from when you inherit from models.Manager.
phillc