Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » get_object_or_404 on composite key and Value Error:Invalid Literal for int with base 10 [RSS Feed]

#1 Dec. 1, 2010 18:20:45

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

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


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.




--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@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 Dec. 1, 2010 18:57:04

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

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


On Dec 1, 6:20 pm, 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:
>            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.

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.

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@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 Dec. 1, 2010 19:10:23

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

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


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, MemOrgId
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

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@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 Dec. 2, 2010 07:45:36

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

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


On Dec 2, 12:10 am, bruno desthuilliers
<bruno.desthuilli...@gmail.com> wrote:
> 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, MemOrgId
>     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

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.

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@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 Dec. 2, 2010 09:58:02

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

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


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

On Thu, Dec 2, 2010 at 7:45 AM, Pranav <pranav...@gmail.com> wrote:
>
>
> On Dec 2, 12:10 am, bruno desthuilliers
> <bruno.desthuilli...@gmail.com> wrote:
>> 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, MemOrgId
>>     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
>
> 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.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To post to this group, send email to django-us...@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-us...@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

  • Root
  • » Django
  • » get_object_or_404 on composite key and Value Error:Invalid Literal for int with base 10 [RSS Feed]

Board footer

Moderator control

Enjoy the 24th 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