Re: Primary key for read-only models

2012-01-14 Thread JohnA
I think you can do what you want as long as you set your read-only
model to managed=False in the Meta.  You have to set some field to
primary_key=True but it doesn’t have to actually be unique.  To avoid
situations where django will do something that will only work if it is
unique you might want to put the primary_key=True on a field you never
use for lookup.

You’ll have to create and populate the unmanaged table yourself, and
create whatever indices you think will be helpful in doing lookups. (I
assume you were planning that anyway.)  I believe the decision as to
what if any indices to use for a given lookup (i.e. select) is
entirely internal to the db and is not controlled or influenced by
django.  If you always do lookup via unique combinations of fields,
and you create an index on the combination, you’d expect the db to use
that index, but you never know.

I haven’t done this for real but I might have to down the road so I
did a little test.

# model

class NoPrimaryKey(models.Model):
class Meta:
managed=False
db_table='my_noprimarykey_table'
unique_together=('charField','integerField')
charField = models.CharField(max_length=32,primary_key=True)
integerField = models.IntegerField()
dateField = models.DateField(null=True)

def __unicode__(self):
return u'%s %s' % (self.charField,self.integerField)

# test script

from myapp.models import NoPrimaryKey

def main():
for c,i in (('record 3',300),('record 3',301),):
obj = NoPrimaryKey.objects.get(charField=c,integerField=i)
print obj
main()

# contents of db:

sqlite> select * from my_noprimarykey_table;
record 1|100|
record 2|200|
record 3|300|
record 3|301|
sqlite>

# output of test script

record 3 300
record 3 301

Note that I did this with sqlite.  It can’t promise it will work with
mysql or other backend.



On Jan 12, 10:03 am, Demetrio Girardi 
wrote:
> I need to read data from an "external" database table from my django
> project. I am not interested in modifying the data, only reading it.
> Of course I would like to create a django model for the table, because
> it makes life so much more easier.
>
> I have already done this previously, however in this case the table
> has a multiple field primary key, unsupported by Django. There is no
> other field which is guaranteed to be unique that I can use as primary
> key in the Django model.
>
> Do I need to worry about this? or can I just slap the primary_key flag
> on any of the fields, since I will never be inserting or updating in
> that table?

-- 
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: Primary key for read-only models

2012-01-13 Thread Simone Federici
Try this:
https://github.com/simone/django-compositekey/wiki

and send me feedback :-)


On Thu, Jan 12, 2012 at 16:03, Demetrio Girardi
wrote:

> I need to read data from an "external" database table from my django
> project. I am not interested in modifying the data, only reading it.
> Of course I would like to create a django model for the table, because
> it makes life so much more easier.
>
> I have already done this previously, however in this case the table
> has a multiple field primary key, unsupported by Django. There is no
> other field which is guaranteed to be unique that I can use as primary
> key in the Django model.
>
> Do I need to worry about this? or can I just slap the primary_key flag
> on any of the fields, since I will never be inserting or updating in
> that table?
>
> --
> 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: Primary key for read-only models

2012-01-12 Thread Thorsten Sanders
Had recently kinda the same just that I have 2 tables without a primary 
key at all and I just declared one of the fields as primary key and 
works fine.


On 12.01.2012 16:03, Demetrio Girardi wrote:

I need to read data from an "external" database table from my django
project. I am not interested in modifying the data, only reading it.
Of course I would like to create a django model for the table, because
it makes life so much more easier.

I have already done this previously, however in this case the table
has a multiple field primary key, unsupported by Django. There is no
other field which is guaranteed to be unique that I can use as primary
key in the Django model.

Do I need to worry about this? or can I just slap the primary_key flag
on any of the fields, since I will never be inserting or updating in
that table?

   


--
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: Primary key for read-only models

2012-01-12 Thread Javier Guerra Giraldez
On Thu, Jan 12, 2012 at 10:03 AM, Demetrio Girardi
 wrote:
> I have already done this previously, however in this case the table
> has a multiple field primary key, unsupported by Django. There is no
> other field which is guaranteed to be unique that I can use as primary
> key in the Django model.
>
> Do I need to worry about this? or can I just slap the primary_key flag
> on any of the fields, since I will never be inserting or updating in
> that table?

maybe since it's read-only you could create a view with a field that
concatenates the parts of the composite key

-- 
Javier

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



Primary key for read-only models

2012-01-12 Thread Demetrio Girardi
I need to read data from an "external" database table from my django
project. I am not interested in modifying the data, only reading it.
Of course I would like to create a django model for the table, because
it makes life so much more easier.

I have already done this previously, however in this case the table
has a multiple field primary key, unsupported by Django. There is no
other field which is guaranteed to be unique that I can use as primary
key in the Django model.

Do I need to worry about this? or can I just slap the primary_key flag
on any of the fields, since I will never be inserting or updating in
that table?

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