Django 1 7 Makemigration non-nullable field

0 votes
I am trying to use django-orderedmodel in my project.

But, Running makemigrations doesn't work and it shows the following:

 You are trying to add a non-nullable field 'order' to slide without a default; we can't do that (the database needs something to populate existing rows).
Please select a fix:
 1) Provide a one-off default now (will be set on all existing rows)
 2) Quit, and let me add a default in models.py
Select an option:
 

Can anyone tell me what should I do to fix this?
May 9, 2019 in Python by ana1504.k
• 7,910 points
4,162 views

1 answer to this question.

0 votes
As the order field is unique, you'll need to add the field in several migration steps, replacing the original operations in your migration:

Add a nullable field, set the default to NULL.
Set the field to a unique value in each row.
Add a NOT NULL constraint.
 

You can use something like this:

operations = [
    migrations.AddField('myapp.MyModel', 'order', models.PositiveIntegerField(null=True, unique=True)),
    migrations.RunPython(set_order),
    migrations.AlterField('myapp.MyModel', 'order', models.PositiveIntegerField(blank=True, unique=True)),
]
answered May 9, 2019 by SDeb
• 13,300 points

Related Questions In Python

0 votes
1 answer
0 votes
1 answer

Need help installing easy_install in Python 2.7.1 on Windows 7

That tool is part of the setuptools ...READ MORE

answered Dec 26, 2018 in Python by Nymeria
• 3,560 points
1,206 views
+1 vote
1 answer

How to install tensorflow using anaconda and python 3.7.1 on windows?

Since I am using python 3.5 so ...READ MORE

answered Aug 19, 2019 in Python by Arvind
• 3,050 points
9,319 views
0 votes
0 answers

AWS: can't connect to RDS database from my machine

When I tried to debug the code ...READ MORE

Apr 9, 2022 in Others by Kichu
• 19,040 points
823 views
0 votes
1 answer

how to download and install Django rest framework?

To install Django, you can simply open ...READ MORE

answered Apr 24, 2018 in Python by Christine
• 15,790 points
1,998 views
0 votes
1 answer
0 votes
1 answer

How to install django 1.4?

This can be done by using this ...READ MORE

answered Jul 11, 2019 in Python by SDeb
• 13,300 points
886 views
0 votes
1 answer

How is Python 2.7.3 and Python 3.3 different?

raw_input() is not used in Python 3. Use input()  ...READ MORE

answered Sep 12, 2018 in Python by SDeb
• 13,300 points
935 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP