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