Re: Reading data from database tied to another Django project?

2009-07-17 Thread Benjamin Kreeger

Thanks for your help, Ian. Two awesome solutions; I'll try and check
'em out today if I get the chance.

Ben

On Jul 15, 1:26 pm, Ian Clelland  wrote:
> On Jul 14, 8:23 am,BenjaminKreeger 
> wrote:
>
> > I was using PostgreSQL at first, but then I found information about
> > reading data across databases only with MySQL, so I'm using MySQL for
> > the time being.
>
> > Now, I don't have a database set for P1 yet because it's just
> > displaying reStructuredText files; if I were to set up that site to
> > access P2's database (in P1's settings.py file), would I be able to
> > (eek) do raw SQL to get the data to display the way I want? Or would
> > there be an easier way to do that WITHOUT raw SQL, and WITHOUT
> > duplicating that model declared on P2?
>
> I didn't realise that P1 didn't have a database at all -- if that's
> the case, then you can just use the credentials from P2 (the
> DATABASE_* lines from settings.py) in P1's settings. You can define
> the same models -- it's as simple as installing the same apps that P2
> uses, there should be no duplication of code, and you'll have access
> to all of the P2 data from P1.
>
> If P1 needs its own database, however, (and you're using MySQL, and
> your databases are on the same server), then you can use this patch to
> django/db/backends/mysql/base.py (this is based on the 1.0.x branch;
> other branches should be similar)
>
> ===
> --- django/db/backends/mysql/base.py    (revision 10742)
> +++ django/db/backends/mysql/base.py    (working copy)
> @@ -143,7 +143,7 @@
>      def quote_name(self, name):
>          if name.startswith("`") and name.endswith("`"):
>              return name # Quoting once is enough.
> -        return "`%s`" % name
> +        return ".".join(["`%s`" % name_part for name_part in
> name.split(".")])
>
>      def random_function_sql(self):
>          return 'RAND()'
>
> That will let you define, in your P1 models, a complete table name
> like this:
>
> class Example(models.Model):
>     ... fields ...
>
>     class Meta:
>         db_table = 'p2_database.table_name'
>
> And mysql will get the data from the right database. No raw SQL
> required.
>
> Ian
--~--~-~--~~~---~--~~
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: Reading data from database tied to another Django project?

2009-07-15 Thread Ian Clelland



On Jul 14, 8:23 am, Benjamin Kreeger 
wrote:
> I was using PostgreSQL at first, but then I found information about
> reading data across databases only with MySQL, so I'm using MySQL for
> the time being.
>
> Now, I don't have a database set for P1 yet because it's just
> displaying reStructuredText files; if I were to set up that site to
> access P2's database (in P1's settings.py file), would I be able to
> (eek) do raw SQL to get the data to display the way I want? Or would
> there be an easier way to do that WITHOUT raw SQL, and WITHOUT
> duplicating that model declared on P2?

I didn't realise that P1 didn't have a database at all -- if that's
the case, then you can just use the credentials from P2 (the
DATABASE_* lines from settings.py) in P1's settings. You can define
the same models -- it's as simple as installing the same apps that P2
uses, there should be no duplication of code, and you'll have access
to all of the P2 data from P1.

If P1 needs its own database, however, (and you're using MySQL, and
your databases are on the same server), then you can use this patch to
django/db/backends/mysql/base.py (this is based on the 1.0.x branch;
other branches should be similar)

===
--- django/db/backends/mysql/base.py(revision 10742)
+++ django/db/backends/mysql/base.py(working copy)
@@ -143,7 +143,7 @@
 def quote_name(self, name):
 if name.startswith("`") and name.endswith("`"):
 return name # Quoting once is enough.
-return "`%s`" % name
+return ".".join(["`%s`" % name_part for name_part in
name.split(".")])

 def random_function_sql(self):
 return 'RAND()'


That will let you define, in your P1 models, a complete table name
like this:

class Example(models.Model):
... fields ...

class Meta:
db_table = 'p2_database.table_name'

And mysql will get the data from the right database. No raw SQL
required.

Ian
--~--~-~--~~~---~--~~
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: Reading data from database tied to another Django project?

2009-07-14 Thread Benjamin Kreeger

I was using PostgreSQL at first, but then I found information about
reading data across databases only with MySQL, so I'm using MySQL for
the time being.

Now, I don't have a database set for P1 yet because it's just
displaying reStructuredText files; if I were to set up that site to
access P2's database (in P1's settings.py file), would I be able to
(eek) do raw SQL to get the data to display the way I want? Or would
there be an easier way to do that WITHOUT raw SQL, and WITHOUT
duplicating that model declared on P2?

Ben

On Jul 10, 3:58 pm, Ian Clelland  wrote:
> On Jul 10, 12:37 pm, Ben Kreeger  wrote:
>
> > How do I go about accessing that data from P2's database? Do I need to
> > create a model in P1 and bind it to a certain table in P2's database?
> > If that's the case, how do I specify access credentials for that
> > database? Is that in settings.py?
>
> If the two projects live in completely separate databases, then there
> is no easy way to do that (yet -- there's a GSoC project being
> actively worked on by Alex Gaynor to provide multi-database support in
> Django)
>
> In a similar situation, where both projects had a database on a common
> mysql server, I managed to do this with a one-line change to the
> Django mysql backend, which allowed me to specify a table in the
> model's Meta class as "database.table" -- this only works because
> MySQL allows you to access tables in one database from a connection to
> another, as long as your credentials are valid for both.
>
> Another (mysql-only, unfortunately) solution is to run mysqlproxy in
> front of your P2 database, and have it direct requests for P1 tables
> to the P1 database server. You would have to be careful to avoid any
> sort of queries that tried to join the tables from the two databases
> together, though -- avoiding foreign keys from P1 models to P2 models
> should be enough.
>
> Ian
--~--~-~--~~~---~--~~
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: Reading data from database tied to another Django project?

2009-07-10 Thread Ian Clelland

On Jul 10, 12:37 pm, Ben Kreeger  wrote:
> How do I go about accessing that data from P2's database? Do I need to
> create a model in P1 and bind it to a certain table in P2's database?
> If that's the case, how do I specify access credentials for that
> database? Is that in settings.py?

If the two projects live in completely separate databases, then there
is no easy way to do that (yet -- there's a GSoC project being
actively worked on by Alex Gaynor to provide multi-database support in
Django)

In a similar situation, where both projects had a database on a common
mysql server, I managed to do this with a one-line change to the
Django mysql backend, which allowed me to specify a table in the
model's Meta class as "database.table" -- this only works because
MySQL allows you to access tables in one database from a connection to
another, as long as your credentials are valid for both.

Another (mysql-only, unfortunately) solution is to run mysqlproxy in
front of your P2 database, and have it direct requests for P1 tables
to the P1 database server. You would have to be careful to avoid any
sort of queries that tried to join the tables from the two databases
together, though -- avoiding foreign keys from P1 models to P2 models
should be enough.

Ian
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---