Wednesday, December 1, 2010

Re: 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-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment