get_object_or_404 on composite key and Value Error:Invalid Literal for int with base 10

237 views
Skip to first unread message

Pranav

unread,
Dec 1, 2010, 1:20:39 PM12/1/10
to Django users
Hi all,

I'm new to django and python and i'm working on a project that works
with a legacy database.
I've a particular problem with a "composite key" and "get" or
"get_object_or_404".

i generated the below model form the legacy database using inspectdb

model:
------------------
class Member:
member_id = field.CharField(max_length=20)
organization_id = field.Foreignkey(Organization)
member_type = field.CharField(max_length=10)
class Meta:
db_table = u'member'
unique_together =
(('member_id','organization_id'),)
# unique_together is a constraint i added in order to form the
composite primary key

class Organization:
organization_id = field.CharField(max_length=2)
organization_name = field.CharField(max_length=20)
class Meta:
db_table = u'organization'

I have a function in my view which receives a "MemOrgId" which is
nothing but the composite key of both member_id and organization_id.
for example the value i get is MemOrgId='AA1001', now the problem is
when i try to use get or a get_object_or_404 i get a Value
Error:Invalid Literal for int with base 10.

obj = get_object_or_404(Member,pk = MemOrgId)

i assign the object instance to my form and save the form if the form
is valid.

The Trace Back starts with my view and ends at "fields.__init__."
which retuns int(value).

According to my understanding of the trace back its not able to
convert the key "CP1001" to int coz it contains char data along with
int. I cannot modify the database and i cannot change the way MemOrgId
comes to the view function. Is there a way around this problem????

Thanks for your help.

Regards,
Pranav Hegde.




Daniel Roseman

unread,
Dec 1, 2010, 1:56:58 PM12/1/10
to Django users
You have misunderstood what `unique_together` does. It simply adds a
unique constraint to the table: it does not create a composite primary
key. The actual field referred to by `pk` is still simply `id`. Django
does not support composite primary keys.
--
DR.

bruno desthuilliers

unread,
Dec 1, 2010, 2:10:16 PM12/1/10
to Django users


On 1 déc, 19:20, Pranav <pranav...@gmail.com> wrote:
> Hi all,
>
> I'm new to django and python and i'm working on a project that works
> with a legacy database.
> I've a particular problem with a "composite key" and "get" or
> "get_object_or_404".
>
> i generated the below model form the legacy database using inspectdb
>
> model:
> ------------------
> class Member:

This should inherit from models.Model

>            member_id = field.CharField(max_length=20)
>            organization_id = field.Foreignkey(Organization)
>            member_type = field.CharField(max_length=10)
>            class Meta:
>                     db_table = u'member'
>                     unique_together =
> (('member_id','organization_id'),)
> # unique_together is a constraint i added in order to form the
> composite primary key

It's a composite key, but it's not a primary key - or at least it's
not recognized as such by Django (hint: Django doesn't handle
composite primary keys so far)

> class Organization:

idem

>
> I have a function in my view which receives a "MemOrgId" which is
> nothing but the composite key of both member_id and organization_id.
> for example the value i get is MemOrgId='AA1001', now the problem is
> when i try to use get or a get_object_or_404 i get a Value
> Error:Invalid Literal for int with base 10.
>
> obj = get_object_or_404(Member,pk = MemOrgId)


Since you didn't explicitly declared a primary key for your Member
model, Django automagically adds one, named "id" and defined as an
autoincrement integer, so the 'pk' shortcut resolves to this int
field.

> According to my understanding of the trace back its not able to
> convert the key "CP1001" to int coz it contains char data along with
> int. I cannot modify the database

Should not be a major problem.

> and i cannot change the way MemOrgId
> comes to the view function.

Why ?

> Is there a way around this problem????

Yes: split the compound key and do an explicit lookup:


def yourview(request, MemOrgId):
oid, mid = MemOrgId[0:2], MemOrgId[2:]
obj = get_object_or_404(
organization_id=oid,
member_id=mid
)


IRL you probably want a bit more validation on what the MemOrgId arg
looks like - but this can be done with the correct regexp in your
urls.py

Pranav

unread,
Dec 2, 2010, 2:45:28 AM12/2/10
to Django users


On Dec 2, 12:10 am, bruno desthuilliers
Thanks your solution worked, but now i have a bigger problem i cant
seem to save my form.
Django adds a automatic "id" to the table which i don't have in my
database so my database is throwing an error. Also none of the fields
in my table are unique so cannot set manual primary_key=True, I cannot
change the database as I said earlier. Is there a way to ignore this
auto generated id or a way to use the create a table without any
primary keys.

Tom Evans

unread,
Dec 2, 2010, 4:57:52 AM12/2/10
to django...@googlegroups.com
No, django does not support composite primary keys. So trying to use
composite primary keys will not work. The best you can accomplish is a
constraint.

HTH

Tom

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

Reply all
Reply to author
Forward
0 new messages