0

I am creating an object like so,

order = Order.objects.create(created_by=anon_user,modified_by=anon_user)

i am getting an error orders_order.phone may not be NULL. phone is one of many fields that belong to the Order model.

i have failed(logically) to include the phone field as i create the Order object.

Any suggestions.

6
  • 3
    Include the field when you create the object. Commented Feb 15, 2013 at 19:26
  • Use same logic like the question what I have been already answered for you.stackoverflow.com/questions/14899334/… Commented Feb 15, 2013 at 19:45
  • If you dont want to pass phone object in the Order model allow null for it. Commented Feb 15, 2013 at 19:46
  • Alternatively, set those fields to blank and enforce their presence with a ModelForm if necessary. Commented Feb 15, 2013 at 19:46
  • @IgnacioVazquez-Abrams that is what i want help with, because i have tried order = Order.objects.create(created_by=anon_user,modified_by=anon_user,phone=phone)and it has failed.Maybe i just do not know how.. Commented Feb 15, 2013 at 19:49

2 Answers 2

1

Django requires your phone field to not be null right now. This also means you can't pass phone=None, so if your phone variable that you're passing into your Order object is None, it will fail.

Your options are:

  1. Set a default value on the model
  2. Allow null on the model
  3. Pass in a non-null value to the model upon creation
Sign up to request clarification or add additional context in comments.

Comments

0

Assuming from your question the model requires that phone number be there on creation.

Things you can do.

  1. phone_number = models.PhoneField(null=True, blank=True)
  2. phone_number = models.PhoneField(default='6132760547')

Those to model changes should help first is saying that the field can be blank as in contain an empty or None

Second is if nothing is passed default value is that number.

1 Comment

', i had fixed the problem by the time you posted your answer, i went with the first second option.

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.