The inline foreign key did not match the parent instance primary key.

229 views
Skip to first unread message

Ajay Kumar

unread,
May 9, 2015, 5:54:40 AM5/9/15
to django...@googlegroups.com
I'm trying to set custom id for my model,

models.py

from django.db import models
import uuid


def get_ref_id():
 ref_id
= str(uuid.uuid4())[:11].replace('-','').lower()
 
try:
 id_exists
= Sample.objecsts.get(ref_id = ref_id)
 get_ref_id
()
 
except:
 
return ref_id


class Sample(models.Model):
 ref_id
= models.CharField(max_length = 128, primary_key = True, default=get_ref_id)
 email
= models.EmailField(max_length=128,unique=True)


 
def __unicode__(self):
 
return self.email


class MobileNumber(models.Model):
 sample
= models.ForeignKey(Sample)
 mobile_number
= models.CharField(max_length=128)


admin.py

from django.contrib import admin
from sample.models import Sample, MobileNumber


class MobileNumberInline(admin.StackedInline):
 model
= MobileNumber
 extra
= 1


class SampleAdmin(admin.ModelAdmin):
 inlines
= [MobileNumberInline]


admin
.site.register(Sample,SampleAdmin)



When i try to insert a record from admin it shows the error, But when i tried only with parent table it works.When i came to add child table its not working.Its just shows "Please correct the error below".And then i tried to save through userinterface views(out of admin) it shows the following error "The inline foreign key did not match the parent instance primary key". 





Reply all
Reply to author
Forward
0 new messages