Model Manager QuerySet over-ride class not working as expected

39 views
Skip to first unread message

rmschne

unread,
Nov 2, 2014, 1:12:10 PM11/2/14
to django...@googlegroups.com
I've upgraded to Django 1.7.1  

Following the upgrade most (haven't tested all, but for all tested this true) the over-rided queryset is not functioning.  

when the code calls
qs=Member.Active_objects.all()

then all members are returned, not just those which confrom to the filter specified in the Class MemberActive(models.Manager)

this all worked for many years and only now works following upgrade to 1.7.1.  I upgraded just to keep up.  Now regret that decision, but difficult to go back.

I've tried both get_query_set() and get_queryset() ... I understand the latter is required in 1.7.

Any suggestions?


===the code snippets:

class MemberActive(models.Manager):
    def get_guery_set(self):
        qs=super(MemberActive, self).get_query_set().filter(status='Active')
        return qs

class Member(models.Model):
    membertype=models.ForeignKey(Membertype,related_name='members',db_column='mtypeid', null=True, on_delete=models.SET_NULL,help_text='Type of member.',verbose_name='Member Type')
    lname = models.CharField(max_length=150, db_column='lame', blank=True, help_text='Last name.',verbose_name='Last Name') 
    fname = models.CharField(max_length=150, db_column='fame', blank=True, help_text='First name.',verbose_name='First Name') 
    status = models.CharField(max_length=12,db_column="status", blank=True,help_text="Membership status)

    objects = models.Manager() # the default manager

    Active_objects = MemberActive()
 

rmschne

unread,
Nov 2, 2014, 1:25:57 PM11/2/14
to django...@googlegroups.com
Also, when I call from the calling programm:

qs=Member.all().filter(status='Active') ... then just those records where status=Active are returned
qs=Member.Active.all() ... all records from data base returned ... but expected only status=Active

Collin Anderson

unread,
Nov 2, 2014, 8:51:59 PM11/2/14
to django...@googlegroups.com
Hello

You are saying this doesn't work as expected?

class MemberActive(models.Manager):
   
def get_queryset(self):
        qs
= super(MemberActive, self).get_queryset().filter(status='Active')
       
return qs

class Member(models.Model):
   
# etc
   
Active_objects = MemberActive()

assert all(m.status == 'Active' for m in Member.Active_objects.all())

Collin

rmschne

unread,
Nov 3, 2014, 2:17:12 AM11/3/14
to django...@googlegroups.com
Collin,

Thanks ... no I'm not saying that as I did do any assert statements.  When I add that to the main calling programme, it fails with an "Assertion Error".  Having not used assert statements before, I'm not sure what that means.  Can you help?

What I am saying is that when from the calling programme, I call the main object with a filter, it works as expected--only members with 'Active' status.  When I call the custom class (Class MemberActive) with no filter (as the filter is in the custom class), I get all records regardless of status, e.g. the custom class is running without regard to the def statement.

rmschne

unread,
Nov 3, 2014, 3:41:55 AM11/3/14
to django...@googlegroups.com
Colin,

Thanks... no, I'm saying anything about an "assert" statement as I did not use it.  I'm saying that
: when I call the main class object ("class Member"), the return is all records in the database (as expected). 
: when I call the custom subclass object (class MemberActive), I expect a filtered list, but it returns all records (not expected and "the bug")
: when I call the custom subclass object (class MemberActive), and filter it, the return is only active members (as expected)

I've never used asserts. I added your assert line into the code and Python fails. I know little about asserts and will have to research what that's all about.  Probably a clue.


On Monday, 3 November 2014 01:51:59 UTC, Collin Anderson wrote:

Daniel Roseman

unread,
Nov 3, 2014, 5:12:10 AM11/3/14
to django...@googlegroups.com
On Sunday, 2 November 2014 18:12:10 UTC, rmschne wrote:
Is this your real code? You have two typos in the name of the overridden method: it should be `get_queryset`, not `get_guery_set` (you have a g for a q, and an extra underscore).
--
DR. 

rmschne

unread,
Nov 3, 2014, 5:20:53 AM11/3/14
to django...@googlegroups.com
Daniel,

Gosh. Tail between legs. Clearly, my eyes aren't what they used to be. Not, and should have been, using my Retina screen for this work! Well spotted. Yes this is real code (edited down). Yes, I have "g" in the real code rather than a "q". When I fixed that (global fixing 62 errors), all is well.

What I did (not excuses...just what happened which in engineering is important to understand!): In reading the documentation for the upgrade I saw that get_query_set() was changed to get_queryset(), i.e. the "_" removed. I did a global search and replace in the model.py file, but mistakenly put in "get_gueryset()" as new code.  I didn't then and continued till now not notice.  Interestingly, neither did Django or Python complain (far as I can tell). Replacing the "g" for a "q" it now works as expected.

Thanks!!!!!!!!!

--rms
Reply all
Reply to author
Forward
0 new messages