Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.

#1 Jan. 16, 2011 13:04:49

r.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Query Set involving Model Method


Following is an extract of a Model I'm using. I'd like to be able to
query it to get a query set return that uses one of the Model
Definitions, e.g. get all the Hosts where "complete=True" where
'complete' is a model definition.

I know the nomenclature to use when exclude/include on a field in the
model, e.g. using "sponsor"
qs=DinnerHost.objects.exclude(sponsor=False)

But I want to use the model definition, along the lines suggested by
(but does not work) to get all the completed hosts:

qs=DinnerHost.objects.filter(complete=True)
and tried
gs=DinnerHost.objects.filter(complete()=True

What is the right nomenclature to get this?

Thanks

===========


Backup:

class DinnerHost(models.Model):
member = models.ForeignKey(Member, null=True,
db_column='memberid', related_name="hosts", blank=True)
meeting = models.ForeignKey(Meeting, null=True,
db_column="meetingid", related_name="hosts", blank=True)
host = models.CharField(max_length=300, db_column='Host',
blank=True)
sponsor = models.BooleanField(db_column='Sponsor', blank=True)

def complete(self):
# rules for what makes a complete booking
if self.sponsor and self.isadreceived and self.isfullybooked:
return True
if not self.sponsor and self.isfullybooked:
return True
return False

objects = models.Manager()


Note;
1. not all fields show, for simplicity
2. isfullybooked and isadreceived are both Model methods.

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#2 Jan. 16, 2011 13:29:16

a.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Query Set involving Model Method


Hi,

On Jan 16, 1:04 pm, rmschne <rmsc...@gmail.com> wrote:
> qs=DinnerHost.objects.filter(complete=True)
> and tried
> gs=DinnerHost.objects.filter(complete()=True

Try something along the lines of,

class DinnerHostManager(models.Manager):
def complete(self):
return self.filter(
sponsor=True,
isadreceived=True,
isfullybooked=True)

class DinnerHost(models.Model):
member = models.ForeignKey(Member, null=True,
db_column='memberid', related_name="hosts", blank=True)
meeting = models.ForeignKey(Meeting, null=True,
db_column="meetingid", related_name="hosts", blank=True)
host = models.CharField(max_length=300, db_column='Host',
blank=True)
sponsor = models.BooleanField(db_column='Sponsor', blank=True)

objects = DinnerHostManager()


# Do the selection
gs=DinnerHost.objects.complete()

Although the filter above needs a little work ;-)

aid

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#3 Jan. 16, 2011 14:30:00

r.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Query Set involving Model Method


Thanks. I'm still messing with this; but don't think it quite what I'm
looking for.

I want/need the Model Definition function complete() for use at the
record level. I did this as a function since the rules are not based
simply on the value of the fields.

Ideally, I'd like to have the class DinnerHoseManager(modes.Manager)
use the complete() function to help return the query set for where
complete()=True. How to put that into this class?

It's not the simple filter. Need a little more complexity to
determining "complete" based on the two if statements.

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#4 Jan. 16, 2011 14:51:54

Mikhail K.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Query Set involving Model Method


It doesn't work that way. ORM translates you queries to SQL and the DB
is responsible for filtering. It is not possible to translate
arbitrary python function to SQL or pass it to DB engine. So you have
to formulate you query using .filter syntax or raw SQL.

Another possibility is to denormalize data: add 'complete' field to
the model and recalculate it on save (or whenever it make sense). This
is often a good solution and it is better than complex queries in
terms of performance.

On 16 янв, 19:16, rmschne <rmsc...@gmail.com> wrote:
> Thanks. I'm still messing with this; but don't think it quite what I'm
> looking for.
>
> I want/need the Model Definition function complete() for use at the
> record level.  I did this as a function since the rules are not based
> simply on the value of the fields.
>
> Ideally, I'd like to have the class DinnerHoseManager(modes.Manager)
> use the complete() function to help return the query set for where
> complete()=True.  How to put that into this class?
>
> It's not the simple filter. Need a little more complexity to
> determining "complete" based on the two if statements.

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#5 Jan. 16, 2011 15:04:08

r.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Query Set involving Model Method


Mikhail,

Thanks. I like that idea. Simple and understandable for those who
follow me.

On Jan 16, 2:51 pm, Mikhail Korobov <kmik...@googlemail.com> wrote:
> It doesn't work that way. ORM translates you queries to SQL and the DB
> is responsible for filtering. It is not possible to translate
> arbitrary python function to SQL or pass it to DB engine. So you have
> to formulate you query using .filter syntax or raw SQL.
>
> Another possibility is to denormalize data: add 'complete' field to
> the model and recalculate it on save (or whenever it make sense). This
> is often a good solution and it is better than complex queries in
> terms of performance.
>
> On 16 янв, 19:16, rmschne <rmsc...@gmail.com> wrote:
>
>
>
>
>
>
>
> > Thanks. I'm still messing with this; but don't think it quite what I'm
> > looking for.
>
> > I want/need the Model Definition function complete() for use at the
> > record level.  I did this as a function since the rules are not based
> > simply on the value of the fields.
>
> > Ideally, I'd like to have the class DinnerHoseManager(modes.Manager)
> > use the complete() function to help return the query set for where
> > complete()=True.  How to put that into this class?
>
> > It's not the simple filter. Need a little more complexity to
> > determining "complete" based on the two if statements.

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#6 Jan. 16, 2011 19:39:18

M.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Query Set involving Model Method


try:

gs=DinnerHost.objects.all().filter(complete()=True

objects by default doesn't select anything, so either use
objects.get(pk=var_pk) or the a-typical case all().

-Matteius

On Jan 16, 7:04 am, rmschne <rmsc...@gmail.com> wrote:
> Following is an extract of a Model I'm using.  I'd like to be able to
> query it to get a query set return that uses one of the Model
> Definitions, e.g. get all the Hosts where "complete=True" where
> 'complete' is a model definition.
>
> I know the nomenclature to use when exclude/include on a field in the
> model, e.g. using "sponsor"
>try qs=DinnerHost.objects.exclude(sponsor=False)
>
> But I want to use the model definition, along the lines suggested by
> (but does not work) to get all the completed hosts:
>
> qs=DinnerHost.objects.filter(complete=True)
> and tried
> gs=DinnerHost.objects.filter(complete()=True
>
> What is the right nomenclature to get this?
>
> Thanks
>
> ===========
>
> Backup:
>
> class DinnerHost(models.Model):
>     member = models.ForeignKey(Member, null=True,
> db_column='memberid', related_name="hosts", blank=True)
>     meeting = models.ForeignKey(Meeting, null=True,
> db_column="meetingid", related_name="hosts", blank=True)
>     host = models.CharField(max_length=300, db_column='Host',
> blank=True)
>     sponsor = models.BooleanField(db_column='Sponsor', blank=True)
>
>     def complete(self):
>     # rules for what makes a complete booking
>         if self.sponsor and self.isadreceived and self.isfullybooked:
>             return True
>         if not self.sponsor and self.isfullybooked:
>             return True
>         return False
>
>     objects = models.Manager()
>
> Note;
> 1. not all fields show, for simplicity
> 2. isfullybooked and isadreceived are both Model methods.

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#7 Jan. 17, 2011 06:48:55

Ian C.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Query Set involving Model Method


On Sun, Jan 16, 2011 at 6:51 AM, Mikhail Korobov <kmik...@googlemail.com> wrote:
> It doesn't work that way. ORM translates you queries to SQL and the DB
> is responsible for filtering. It is not possible to translate
> arbitrary python function to SQL or pass it to DB engine. So you have
> to formulate you query using .filter syntax or raw SQL.

This is absolutely correct. You can't mix python functions with SQL functions.

>
> Another possibility is to denormalize data: add 'complete' field to
> the model and recalculate it on save (or whenever it make sense). This
> is often a good solution and it is better than complex queries in
> terms of performance.

There are some downsides to this method, though: It does take up some
space in the database; it requires some processing every time an
object is updated (and if your calculation involves any related
objects, then you have to do it whenever *they* are updated, as well.)
Additionally, if you ever change those rules, then you have to
recalculate the value of that field for every instance in the
database.

That doesn't mean that it's a bad idea -- I use it all the time -- but
you should at least think about it before doing it.

An alternative, if you want to keep the code simple, and don't mind
that it's less efficient than caching the field value, is to pull out
the values from the database with the ORM, and then filter them in
python:

filter(lambda x: x.complete(), DinnerHost.objects.all())

or even like this:




>
> On 16 янв, 19:16, rmschne <rmsc...@gmail.com> wrote:
>> Thanks. I'm still messing with this; but don't think it quite what I'm
>> looking for.
>>
>> I want/need the Model Definition function complete() for use at the
>> record level.  I did this as a function since the rules are not based
>> simply on the value of the fields.
>>
>> Ideally, I'd like to have the class DinnerHoseManager(modes.Manager)
>> use the complete() function to help return the query set for where
>> complete()=True.  How to put that into this class?
>>
>> It's not the simple filter. Need a little more complexity to
>> determining "complete" based on the two if statements.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To post to this group, send email to django-users@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users+unsubscr...@googlegroups.com.
> For more options, visit this group at
>http://groups.google.com/group/django-users?hl=en.
>
>



--
Regards,
Ian Clelland
<clell...@gmail.com>

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#8 Jan. 17, 2011 13:18:19

r.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Query Set involving Model Method


Much thanks to all.

I could not get something like:
gs=DinnerHost.objects.all().filter(complete()=True

to work. probably think fingers on my side to get the nomenclature
write; but I suspect not possible.

While I liked the idea of computing a new field "complete" in the
database which is updated on Save. While initially thought to be a
good idea I changed my mind because:
1. reqireired running the Python/Django program to update ... yet
some of the data in the db gets update via many other tools and so
there will be an consistency issue.
2. writing a stored procedure to update in MysQL ... too complicated
3. doing it as a batch update in Python/Django ... means hitting all
the records and then sort of messes up the value of the timestamp on
each record. I'd rather that timestamp mean something more real than
that.
4. add a related table which does this computation (fixing problem
3) ... too complicated

in the end what I did was in the program where I want to do something
with the "not complete" records, I write a small routine in the
program that extracts a query set with all records, then loops through
all records and finds where, using the Model definition, "if not
h.complete():" where "h" is the record from the loop in the query set.
Then I just append to a list the ID of that record. Then i re-query
the database for those records where ID is in the list.

Works. understandable for those who follow, etc. No db changes and
no added maintenance.


hosts1=DinnerHost.objects.all().filter(year__exact=CURRENT_YEAR)
tablelist=
for h in hosts1:
if not h.complete():
tablelist.append(h.tablename)

hosts2=DinnerHost.objects.filter(year__exact=CURRENT_YEAR).filter(tablename__in=tablelist)

Now hosts2 has what I want.

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

Board footer

Moderator control

Enjoy the 17th of August
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support