Hello,
I've the following problem:
- I have a model (ModelA) with a primary key consisting of a foreignkey
(ModelB - ID)
- *Databasestructure*
TableA
TableA_TableB - PK, BIGINT(20), FK TableB:ID
NAME - VARCHAR(255)
TableB
ID - PK, AI, BIGINT(20)
NAME - VARCHAR(255)
- *Django Models*
class ModelA(models.Model):
ModelA_ModelB = models.ForeignKey("ModelB", primary_key=True)
name = models.CharField(max_length=255)
class ModelB(models.Model):
id = models.BigIntegerField(primary_key=True)
name = models.CharField(max_length=255)
- *Django save_model of ModelA*
def save_model(self, request, obj, form, change):
oObj = ModelB()
oObj.name = "TestIt"
oObj.save()
obj.ModelA_ModelB = oObj
obj.save()
- *Creating a new ModelA using the admin GUI I got the following error*
Django Version:1.4Exception Type:IntegrityErrorException Value:
(1048, "Column 'ModelA_ModelB_id' cannot be null")
- *Model instance reference :
Save<https://docs.djangoproject.com/en/dev/ref/models/instances/?from=olddocs#django.db.models.Model.save>
*
>>> b2 = Blog(name='Cheddar Talk', tagline='Thoughts on cheese.')>>> b2.id
>>> # Returns None, because b doesn't have an ID yet.>>> b2.save()>>> b2.id
>>> # Returns the ID of your new object.
Running the suggested Code above on my model (ModelB) I do not get the ID of
the model after saving it. I got None for ID after saving my model. The
Database contains the saved model.
What am I doing wrong? Why does my model not have a id after saving it to the
Database?
kind regards
--
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/-/iRkInzJcHXAJ.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.