AW: outer joins, raw sql or one-to-many?

2011-05-10 Thread Szabo, Patrick (LNG-VIE)
The latter might not work for your specific model but in general it works. 
I'm using this synax in quite a few cases.


. . . . . . . . . . . . . . . . . . . . . . . . . .
Patrick Szabo
 XSLT Developer 
LexisNexis
Marxergasse 25, 1030 Wien

mailto:patrick.sz...@lexisnexis.at
Tel.: +43 (1) 534 52 - 1573 
Fax: +43 (1) 534 52 - 146 


-Ursprüngliche Nachricht-

Von: django-users@googlegroups.com [mailto:django-users@googlegroups.com] Im 
Auftrag von Michel30
Gesendet: Dienstag, 10. Mai 2011 09:48
An: Django users
Betreff: Re: outer joins, raw sql or one-to-many?

wow this works :-)

I think I mixed up the tables before:

submitter = models.ForeignKey(Author, db_column='SubmitterID')
vs
submitter = models.ForeignKey(Author, db_column='authorid')

the latter doesn't work obviously..

Thanks all, I think this will help me on my way!

On May 10, 9:38 am, "Szabo, Patrick \(LNG-VIE\)"
 wrote:
> The lather should work...
>
> . . . . . . . . . . . . . . . . . . . . . . . . . .
> Patrick Szabo
>  XSLT Developer
> LexisNexis
> Marxergasse 25, 1030 Wien
>
> mailto:patrick.sz...@lexisnexis.at
> Tel.: +43 (1) 534 52 - 1573
> Fax: +43 (1) 534 52 - 146
>
> -Ursprüngliche Nachricht-
>
> Von: django-users@googlegroups.com [mailto:django-users@googlegroups.com] Im 
> Auftrag von Kenneth Gonsalves
> Gesendet: Dienstag, 10. Mai 2011 09:34
> An: django-users@googlegroups.com
> Betreff: Re: outer joins, raw sql or one-to-many?
>
> On Tue, 2011-05-10 at 00:26 -0700, Michel30 wrote:
> > {{ some_thing.firstname }}
>
> maybe
> {{ some_thing.submitter.firstname }}
> --
> regards
> KGhttp://lawgon.livejournal.com
> Coimbatore LUG roxhttp://ilugcbe.techstud.org/
>
> --


-- 
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.



Re: outer joins, raw sql or one-to-many?

2011-05-10 Thread Michel30
wow this works :-)

I think I mixed up the tables before:

submitter = models.ForeignKey(Author, db_column='SubmitterID')
vs
submitter = models.ForeignKey(Author, db_column='authorid')

the latter doesn't work obviously..

Thanks all, I think this will help me on my way!

On May 10, 9:38 am, "Szabo, Patrick \(LNG-VIE\)"
 wrote:
> The lather should work...
>
> . . . . . . . . . . . . . . . . . . . . . . . . . .
> Patrick Szabo
>  XSLT Developer
> LexisNexis
> Marxergasse 25, 1030 Wien
>
> mailto:patrick.sz...@lexisnexis.at
> Tel.: +43 (1) 534 52 - 1573
> Fax: +43 (1) 534 52 - 146
>
> -Ursprüngliche Nachricht-
>
> Von: django-users@googlegroups.com [mailto:django-users@googlegroups.com] Im 
> Auftrag von Kenneth Gonsalves
> Gesendet: Dienstag, 10. Mai 2011 09:34
> An: django-users@googlegroups.com
> Betreff: Re: outer joins, raw sql or one-to-many?
>
> On Tue, 2011-05-10 at 00:26 -0700, Michel30 wrote:
> > {{ some_thing.firstname }}
>
> maybe
> {{ some_thing.submitter.firstname }}
> --
> regards
> KGhttp://lawgon.livejournal.com
> Coimbatore LUG roxhttp://ilugcbe.techstud.org/
>
> --
> 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.

-- 
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.



AW: outer joins, raw sql or one-to-many?

2011-05-10 Thread Szabo, Patrick (LNG-VIE)
The lather should work...


. . . . . . . . . . . . . . . . . . . . . . . . . .
Patrick Szabo
 XSLT Developer 
LexisNexis
Marxergasse 25, 1030 Wien

mailto:patrick.sz...@lexisnexis.at
Tel.: +43 (1) 534 52 - 1573 
Fax: +43 (1) 534 52 - 146 


-Ursprüngliche Nachricht-

Von: django-users@googlegroups.com [mailto:django-users@googlegroups.com] Im 
Auftrag von Kenneth Gonsalves
Gesendet: Dienstag, 10. Mai 2011 09:34
An: django-users@googlegroups.com
Betreff: Re: outer joins, raw sql or one-to-many?

On Tue, 2011-05-10 at 00:26 -0700, Michel30 wrote:
> {{ some_thing.firstname }}

maybe
{{ some_thing.submitter.firstname }}
-- 
regards
KG
http://lawgon.livejournal.com
Coimbatore LUG rox
http://ilugcbe.techstud.org/

-- 
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.



-- 
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.



Re: outer joins, raw sql or one-to-many?

2011-05-10 Thread Kenneth Gonsalves
On Tue, 2011-05-10 at 00:26 -0700, Michel30 wrote:
> {{ some_thing.firstname }}

maybe
{{ some_thing.submitter.firstname }}
-- 
regards
KG
http://lawgon.livejournal.com
Coimbatore LUG rox
http://ilugcbe.techstud.org/

-- 
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.



Re: outer joins, raw sql or one-to-many?

2011-05-10 Thread Michel30
Oops slip of the mouse there :-)

I've tried setting up the relations before like my previous post.
But then when I want to fetch data with, for example:
   some_thing = Documentrevision.objects.filter(some_filter criteria)
and then use it in my form like this:
{{ some_thing.firstname }} 

There is no data while I expected it to follow the relation back to
the Author table..

On May 10, 9:18 am, Michel30  wrote:
> Hi Kenneth,
>
> I tried that before using:
> submitterid = models.ForeignKey('Author', to_field='authorid')
>
> On May 10, 9:07 am, Kenneth Gonsalves  wrote:
>
> > On Mon, 2011-05-09 at 23:56 -0700, Michel30 wrote:
> > > Now, for every documentid I retrieve I want to find it's firstname and
> > > lastname. Authorid relates to submitterid in this case.
>
> > submitter should be foreign key to author - then all your problems will
> > go away.
> > --
> > regards
> > KGhttp://lawgon.livejournal.com
> > Coimbatore LUG roxhttp://ilugcbe.techstud.org/
>
>

-- 
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.



Re: outer joins, raw sql or one-to-many?

2011-05-10 Thread Kenneth Gonsalves
On Tue, 2011-05-10 at 00:18 -0700, Michel30 wrote:
> I tried that before using:
> submitterid = models.ForeignKey('Author', to_field='authorid') 

that is the correct way (although I would prefer to call the field
submitter)
-- 
regards
KG
http://lawgon.livejournal.com
Coimbatore LUG rox
http://ilugcbe.techstud.org/

-- 
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.



Re: outer joins, raw sql or one-to-many?

2011-05-10 Thread Michel30
Hi Kenneth,

I tried that before using:
submitterid = models.ForeignKey('Author', to_field='authorid')

On May 10, 9:07 am, Kenneth Gonsalves  wrote:
> On Mon, 2011-05-09 at 23:56 -0700, Michel30 wrote:
> > Now, for every documentid I retrieve I want to find it's firstname and
> > lastname. Authorid relates to submitterid in this case.
>
> submitter should be foreign key to author - then all your problems will
> go away.
> --
> regards
> KGhttp://lawgon.livejournal.com
> Coimbatore LUG roxhttp://ilugcbe.techstud.org/

-- 
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.



Re: outer joins, raw sql or one-to-many?

2011-05-10 Thread Jani Tiainen
On Mon, 2011-05-09 at 23:56 -0700, Michel30 wrote:
> Hey all,
> 
> I'm having trouble wrapping my head around querying multiple tables
> with relations between them.
> I'm writing an (cms-like) app that will use an existing mysql
> database. I modeled that, got my code up and running and am now in the
> process of trying to retrieve and submit data to it.
> Here is an example of my model:
> 
> class Author(models.Model):
> authorid = models.IntegerField(primary_key=True,
> db_column='AuthorID')
> firstname = models.CharField(max_length=96,
> db_column='FirstName')
> lastname = models.CharField(max_length=96, db_column='LastName')
> 
> class Meta:
> db_table = u'Author'
> def __unicode__(self):
> return self.middleinitials
> 
> class Documentrevision(models.Model):
> docrevid = models.IntegerField(primary_key=True,
> db_column='DocRevID')
> documentid = models.IntegerField(db_column='DocumentID')
> submitterid = models.IntegerField(db_column='SubmitterID')
> ...
> class Meta:
> db_table = u'DocumentRevision'
> def __unicode__(self):
> return self.documenttitle
> 
> Now, for every documentid I retrieve I want to find it's firstname and
> lastname. Authorid relates to submitterid in this case.
> Can someone help me on my way on how to accomplish this?
> 
> Many thanks,
> Michel
> 

How about defining foreign key relation instead of raw integer id
fields? Since, after all, they are related, aren't they?

Then you can use magnificent ORM to do lot of tricks for you - including
retrieving data you just asked. And submitting it. And keeping all that
together.


So instead of submitterid you do following:

submitter = models.ForeignKey(Author, db_column='SubmitterID')

Same for documentid. Then assuming Document - Author relation is
many-to-many you can setup that relation using through
(Documentrevision) option. And now you can quite easily fetch all
authors for each Document.

Note that you should think in terms of ORM. Which means that you have
objects that relate each other, Document, DocumentRevision and Author.
Document has many DocumentRevisions and many authors. 

So in Document model you do something like:

authors = model.ManyToMany(Author, through='DocumentRevision')

And now it's like walk in the park:

for document in Document.objects.all():
for author in document.authors:
print author.firstname, author.lastname


-- 

Jani Tiainen

-- 
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.



Re: outer joins, raw sql or one-to-many?

2011-05-10 Thread Kenneth Gonsalves
On Mon, 2011-05-09 at 23:56 -0700, Michel30 wrote:
> Now, for every documentid I retrieve I want to find it's firstname and
> lastname. Authorid relates to submitterid in this case. 

submitter should be foreign key to author - then all your problems will
go away.
-- 
regards
KG
http://lawgon.livejournal.com
Coimbatore LUG rox
http://ilugcbe.techstud.org/

-- 
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.



outer joins, raw sql or one-to-many?

2011-05-09 Thread Michel30
Hey all,

I'm having trouble wrapping my head around querying multiple tables
with relations between them.
I'm writing an (cms-like) app that will use an existing mysql
database. I modeled that, got my code up and running and am now in the
process of trying to retrieve and submit data to it.
Here is an example of my model:

class Author(models.Model):
authorid = models.IntegerField(primary_key=True,
db_column='AuthorID')
firstname = models.CharField(max_length=96,
db_column='FirstName')
lastname = models.CharField(max_length=96, db_column='LastName')

class Meta:
db_table = u'Author'
def __unicode__(self):
return self.middleinitials

class Documentrevision(models.Model):
docrevid = models.IntegerField(primary_key=True,
db_column='DocRevID')
documentid = models.IntegerField(db_column='DocumentID')
submitterid = models.IntegerField(db_column='SubmitterID')
...
class Meta:
db_table = u'DocumentRevision'
def __unicode__(self):
return self.documenttitle

Now, for every documentid I retrieve I want to find it's firstname and
lastname. Authorid relates to submitterid in this case.
Can someone help me on my way on how to accomplish this?

Many thanks,
Michel

-- 
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.