On Wednesday, 15 June 2011 22:25:24 UTC+1, Satyajit Sarangi wrote: > > his is my models.py > > from django.db import models > > class Sdr_Layer(models.Model): layer_name = > models.CharField(max_length = 100) > layer_attribute_name=models.CharField(max_length=100) > > # This is an auto-generated Django model module created by ogrinspect. > from django.contrib.gis.db import models > > > > > > > > > # This is an auto-generated Django model module created by ogrinspect. > from django.contrib.gis.db import models > > class Test(models.Model): > layer_id= models.ForeignKey(Sdr_Layer) > name = models.CharField(max_length=80) > descriptio = models.CharField(max_length=80) > geom = models.PolygonField(srid=4326) > objects = models.GeoManager() > S = Sdr_Layer(layer_name="Test") > > # Auto-generated `LayerMapping` dictionary for Test model > test_mapping = { > 'name' : 'Name', > 'descriptio' : 'Descriptio', > 'geom' : 'POLYGON25D', > } > S.layer_attribute_name=test_mapping > S.save(); > > When I am trying to load data to Test model , it throws me a not_null > error on layer_id which is the foreign key here . I basically want the > primary key of Sdr_Layer to act as a foreign key here . Sdr_Layer > table does have values . So , why such an error ?
On Wednesday, 15 June 2011 22:25:24 UTC+1, Satyajit Sarangi wrote: > > his is my models.py > > from django.db import models > > class Sdr_Layer(models.Model): layer_name = > models.CharField(max_length = 100) > layer_attribute_name=models.CharField(max_length=100) > > # This is an auto-generated Django model module created by ogrinspect. > from django.contrib.gis.db import models > > # This is an auto-generated Django model module created by ogrinspect. > from django.contrib.gis.db import models > > class Test(models.Model): > layer_id= models.ForeignKey(Sdr_Layer) > name = models.CharField(max_length=80) > descriptio = models.CharField(max_length=80) > geom = models.PolygonField(srid=4326) > objects = models.GeoManager() > S = Sdr_Layer(layer_name="Test") > > # Auto-generated `LayerMapping` dictionary for Test model > test_mapping = { > 'name' : 'Name', > 'descriptio' : 'Descriptio', > 'geom' : 'POLYGON25D', > } > S.layer_attribute_name=test_mapping > S.save(); > > When I am trying to load data to Test model , it throws me a not_null > error on layer_id which is the foreign key here . I basically want the > primary key of Sdr_Layer to act as a foreign key here . Sdr_Layer > table does have values . So , why such an error ? You need to think things through a bit more carefully. It seems like you're randomly assigning things without really having any idea of what you're doing. This code: S = Sdr_Layer(layer_name="Test") instantiates a new Sdr_Layer object, with the name "Test". This line: S.layer_attribute_name=test_mapping tries to do something very strange, which is to assign a dictionary to your CharField. You can do that, but it will be converted to a string on save. Now, none of this code would throw a "not_null error on layer_id", because layer_id is a field on the Test model, which you don't even use in this code. So, there must be code you're not showing us. Also, there is a reason why Python prints the full traceback on an error: it's useful debugging information, not random cruft. Post it too. FWIW, I suspect you wanted to do this: t = Test(**layer_mapping) t.layer_id = S t.save() -- DR. -- You received this message because you are subscribed to the Google Groups "Django users" group. To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/mELn7nUrAOMJ. To post to this group, send email to django-users@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.