1

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.

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Oh, thanks, my brain said I don't want to work any more.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.