Reputation: 2641
I'm getting an object from a json response and I want to use create
method on it so I can save it in the database.
response = requests.get(
settings.BASE_URL,
headers=headers,
params=payload,
)
user_info = response.json()
example_model = ExampleModel.objects.create(api_key=user_info.get('id'))
example_model.save()
the model is:
class ExampleModel(models.Model):
"""
"""
user = models.OneToOneField(User)
example_id = models.CharField(max_length=225, blank=True, null=True)
api_key = models.CharField(max_length=225, blank=True, null=True, validators=[validate_klean_token_syntax])
Follow up is that I'm receiving
Exception Type: IntegrityError at /user-information/
Exception Value: (1048, "Column 'user_id' cannot be null")
Can someone please help me understand why am I getting this error, thanks.
Upvotes: 1
Views: 96
Reputation: 8314
You are trying to save NULL to your field in the model, and it cannot be NULL. You have a relationship between the user field in your ExampleModel and your user model and your model definition is not allowing it to be NULL.
Either allow user in your ExampleModel to be NULL, don't use a reference like OneToOne, or make sure that your get returns user value.
Upvotes: 2