132
You need to provide a default value:
new_field = models.CharField(max_length=140, default='SOME STRING')
132
If you are in early development cycle and don’t care about your current database data you can just remove it and then migrate. But first you need to clean migrations dir and remove its rows from table (django_migrations)
rm your_app/migrations/*
Note: Don’t delete _ _ init _ _ .py in the migrations folder.
rm db.sqlite3
python manage.py makemigrations
python manage.py migrate
If you previously created a superuser for Django’s Admin web app then you might need to create the user again.
- [Django]-How to implement followers/following in Django
- [Django]-Change a Django form field to a hidden field
- [Django]-Nginx doesn't serve static
51
One option is to declare a default value for ‘new_field’:
new_field = models.CharField(max_length=140, default='DEFAULT VALUE')
another option is to declare ‘new_field’ as a nullable field:
new_field = models.CharField(max_length=140, null=True)
If you decide to accept ‘new_field’ as a nullable field you may want to accept ‘no input’ as valid input for ‘new_field’. Then you have to add the blank=True
statement as well:
new_field = models.CharField(max_length=140, blank=True, null=True)
Even with null=True
and/or blank=True
you can add a default value if necessary:
new_field = models.CharField(max_length=140, default='DEFAULT VALUE', blank=True, null=True)
- [Django]-Login Page by using django forms
- [Django]-Django REST framework post array of objects
- [Django]-OneToOneField() vs ForeignKey() in Django
24
In case anyone is setting a ForeignKey
, you can just allow nullable fields without setting a default:
new_field = models.ForeignKey(model, null=True)
If you already have data stored within the database, you can also set a default value:
new_field = models.ForeignKey(model, default=<existing model id here>)
- [Django]-Django Queryset with year(date) = '2010'
- [Django]-Django models: default value for column
- [Django]-Creating email templates with Django
13
If you are early into the development cycle you can try this –
Remove/comment that model and all its usages. Apply migrations. That would delete that model and then add the model again, run migrations and you have a clean model with the new field added.
- [Django]-Django – How to set default value for DecimalField in django 1.3?
- [Django]-Django: Reverse for 'detail' with arguments '('',)' and keyword arguments '{}' not found
- [Django]-Unittest Django: Mock external API, what is proper way?
12
You can’t add reference to table that have already data inside.
Change:
user = models.OneToOneField(User)
to:
user = models.OneToOneField(User, default = "")
do:
python manage.py makemigrations
python manage.py migrate
change again:
user = models.OneToOneField(User)
do migration again:
python manage.py makemigrations
python manage.py migrate
- [Django]-You need to install postgresql-server-dev-X.Y for building a server-side extension or libpq-dev for building a client-side application
- [Django]-Django-rest-framework returning 403 response on POST, PUT, DELETE despite AllowAny permissions
- [Django]-Django-allauth: Linking multiple social accounts to a single user
5
Here is a workaround without having to make compromises such as dropping all existing data/migrations (yikes), requiring two separate migrations, or setting an unwanted default. Take these steps:
- Add the new field to your model with
null=True
python manage.py makemigrations <app_name> --name <migration_name>
- Change the new field to
null=False
python manage.py makemigrations <app_name>
. In this step, you’ll see an option you may never have seen before! You want option 2: "Ignore for now, and let me handle existing rows with NULL myself (e.g. because you added a RunPython or RunSQL operation to handle NULL values in a previous data migration)"- Move the
migrations.AlterField
operation from the second migration into the first one, and add a manual migration to take care of theNULL
values (if you don’t take this step you’ll get integrity errors like before), like so:
def initial_display_names(apps, schema):
Player = apps.get_model('my_app', 'Player')
Player.objects.all().update(display_name='Cool Name')
def reversal(*args):
"""Reversal is NOOP since display_name is simply dropped during reverse"""
class Migration(migrations.Migration):
dependencies = [
('my_app', '0049_foo'),
]
operations = [
migrations.AddField(
model_name='player',
name='display_name',
field=models.CharField(help_text='name as shown in the UI', max_length=256, null=True),
),
migrations.RunPython(initial_display_names, reversal),
migrations.AlterField(
model_name='player',
name='display_name',
field=models.CharField(help_text='name as shown in the UI', max_length=256),
),
]
- Remove the second migration you made.
python manage.py migrate <app_name>
- [Django]-How to run a celery worker with Django app scalable by AWS Elastic Beanstalk?
- [Django]-Django TypeError: get() got multiple values for keyword argument 'invoice_id'
- [Django]-How to query Case-insensitive data in Django ORM?
4
If “website” can be empty than new_field
should also be set to be empty.
Now if you want to add logic on save where if new_field
is empty to grab the value from “website” all you need to do is override the save function for your Model
like this:
class UserProfile(models.Model):
user = models.OneToOneField(User)
website = models.URLField(blank=True, default='DEFAULT VALUE')
new_field = models.CharField(max_length=140, blank=True, default='DEFAULT VALUE')
def save(self, *args, **kwargs):
if not self.new_field:
# Setting the value of new_field with website's value
self.new_field = self.website
# Saving the object with the default save() function
super(UserProfile, self).save(*args, **kwargs)
- [Django]-Django self-referential foreign key
- [Django]-How to use regex in django query
- [Django]-Django admin and MongoDB, possible at all?
3
I was early in my development cycle, so this may not work for everyone (but I don’t see why it wouldn’t).
I added blank=True, null=True
to the columns where I was getting the error. Then I ran the python manage.py makemigrations
command.
Immediately after running this command (and before running python manage.py migrate
), I removed the blank=True, null=True
from all the columns. Then I ran python manage.py makemigrations
again. I was given an option to just change the columns myself, which I selected.
Then I ran python manage.py migrate
and everything worked well!
- [Django]-How to view corresponding SQL query of the Django ORM's queryset?
- [Django]-Override existing Django Template Tags
- [Django]-How to change empty_label for modelForm choice field?
2
In new_file add the boolean property null.
new_field = models.CharField(max_length=140, null=True)
after you run a ./manage.py syncdb
for refresh the DB.
and finally you run ./manage.py makemigrations
and ./manage.py migrate
- [Django]-Django REST Framework : "This field is required." with required=False and unique_together
- [Django]-Can't connect to local MySQL server through socket '/tmp/mysql.sock
- [Django]-Altering one query parameter in a url (Django)
2
Do you already have database entries in the table UserProfile
? If so, when you add new columns the DB doesn’t know what to set it to because it can’t be NULL
. Therefore it asks you what you want to set those fields in the column new_fields
to. I had to delete all the rows from this table to solve the problem.
(I know this was answered some time ago, but I just ran into this problem and this was my solution. Hopefully it will help anyone new that sees this)
- [Django]-Django form fails validation on a unique field
- [Django]-Missing Table When Running Django Unittest with Sqlite3
- [Django]-Filter Queryset on empty ImageField
2
You can use method from Django Doc from this page https://docs.djangoproject.com/en/1.8/ref/models/fields/#default
Create default and use it
def contact_default():
return {"email": "to1@example.com"}
contact_info = JSONField("ContactInfo", default=contact_default)
- [Django]-Django.db.migrations.exceptions.InconsistentMigrationHistory
- [Django]-Django: Using F arguments in datetime.timedelta inside a query
- [Django]-Django – Clean permission table
1
I honestly fount the best way to get around this was to just create another model with all the fields that you require and named slightly different. Run migrations. Delete unused model and run migrations again. Voila.
- [Django]-Django rest framework: query parameters in detail_route
- [Django]-Select distinct values from a table field
- [Django]-Django admin file upload with current model id
1
If you are fine with truncating the table of the model in question, you can specify a one-off default value of None
in the prompt. The migration will have superfluous default=None
while your code has no default. It can be applied just fine because there’s no data in the table anymore which would require a default.
- [Django]-Django index page best/most common practice
- [Django]-Django Generic Views using decorator login_required
- [Django]-What is a "django backend"?
0
What Django actually says is:
Userprofile table has data in it and there might be
new_field
values
which are null, but I do not know, so are you sure you want to mark
property as non nullable, because if you do you might get an error if
there are values with NULL
If you are sure that none of values in the userprofile
table are NULL – fell free and ignore the warning.
The best practice in such cases would be to create a RunPython migration to handle empty values as it states in option 2
2) Ignore for now, and let me handle existing rows with NULL myself (e.g. because you added a RunPython or RunSQL operation to handle NULL values in a previous data migration)
In RunPython migration you have to find all UserProfile
instances with empty new_field
value and put a correct value there (or a default value as Django asks you to set in the model).
You will get something like this:
# please keep in mind that new_value can be an empty string. You decide whether it is a correct value.
for profile in UserProfile.objects.filter(new_value__isnull=True).iterator():
profile.new_value = calculate_value(profile)
profile.save() # better to use batch save
Have fun!
- [Django]-How to check Django version
- [Django]-How to make an auto-filled and auto-incrementing field in django admin
- [Django]-POST jQuery array to Django
0
In models.py
class UserProfile(models.Model):
user = models.OneToOneField(User)
website = models.URLField(blank=True)
new_field = models.CharField(max_length=140, default="some_value")
You need to add some values as default.
- [Django]-How do I use django rest framework to send a file in response?
- [Django]-How can I get MINIO access and secret key?
- [Django]-Django custom management commands: AttributeError: 'module' object has no attribute 'Command'
0
Basically I solved this Issue putting null=True
in added model.
For example:
I was trying to add phone_number in the existing database module.
phone_number = models.CharField(max_length=10)
I previously got this error:
You are trying to add a non-nullable field ‘phone_number’ to customer
without a default; we can’t do that (the database needs something to
populate existing rows). Please select a fix:
- Provide a one-off
default now (will be set on all existing rows with a null value for
this column)- Quit, and let me add a default in models.py
After putting
phone_number = models.CharField(max_length=10, null=True)
python manage.py makemigrations
Migrations for 'collegekhajagharapp':
collegekhajagharapp\migrations\0002_customer_phone_number.py
- Add field phone_number to customer
- [Django]-Filter Queryset on empty ImageField
- [Django]-Define css class in django Forms
- [Django]-How can I get MINIO access and secret key?
0
Delete Model => Makemigrations Models => Migrate Models => Return Back the models => Makemigrations Models => Migrate Models
- [Django]-How do I perform HTML decoding/encoding using Python/Django?
- [Django]-Creating a JSON response using Django and Python
- [Django]-How do I add a placeholder on a CharField in Django?
0
If someone is trying to achieve this and:
- …you are adding a
Foreign Key
field that isNOT NULL
- …you have existing rows in the table
- …and you don’t want to create several migrations
A very minimal example to achieve this, is: Let’s suppose you have an Audio
model and you want it to have a Directory
relationship, you would end up with something like this:
import app.models
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('app', '0016_some_migration'),
]
operations = [
migrations.CreateModel(
name='Directory',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(default='New folder', max_length=200)),
('parent_directory', models.ForeignKey(default=None, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='directories', to='app.directory')),
],
),
migrations.AddField(
model_name='audio',
name='directory',
field=models.ForeignKey(
default=None, # <-
This will fail because this field must have a value
on_delete=django.db.models.deletion.CASCADE,
related_name='files',
to='app.directory',
),
preserve_default=False,
),
]
What I did is:
- In the same migration I created a data migration in which I create a temporary
Directory
model usingmigrations.RunPython()
- Then I use the
default=temp_first_folder["id"]
instead ofdefault=None
- Finally, I run an extra code which will set the actual initial values.
So, yes, for a very brief moment it will have a temporary value, but immediately it will run a second data migration that will set the actual default values for you existing rows
This is how it looks
import app.models
from django.db import migrations, models
import django.db.models.deletion
# This will be used later
temp_first_folder = {"id": None}
def create_temporary_folder(apps, schema_editor, temp_first_folder):
DirectoryModel = apps.get_model("app", "Directory")
temp_directory = DirectoryModel.objects.create(name="Fake folder")
temp_first_folder["id"] = temp_directory.id
def initial_folders(apps, schema_editor):
AudioModel = apps.get_model("app", "Audio")
DirectoryModel = apps.get_model("app", "Directory")
for audio in AudioModel.objects.all():
actual_directory = DirectoryModel.objects.create(
name=f"Folder {audio.id}",
)
audio.directory = actual_directory
audio.save()
# Finally, delete temporary fake folder
DirectoryModel.objects.get(id=temp_first_folder["id"]).delete()
class Migration(migrations.Migration):
dependencies = [
('app', '0016_audio_duration'),
]
operations = [
migrations.CreateModel(
name='Directory',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(default='Nueva carpeta', max_length=200)),
('parent_directory', models.ForeignKey(default=None, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='directories', to='app.directory')),
],
),
#
Let's create a temporary Directory
#
It's important to use a lambda because that will allow you
# to use the temp_first_folder variable
migrations.RunPython(
code=lambda apps, schema_editor: create_temporary_folder(apps, schema_editor, temp_first_folder),
reverse_code=migrations.RunPython.noop
),
# Look the "default" keyword, it's important that is a lambda
# because that will allow you to use temp_first_folder variable
migrations.AddField(
model_name='audio',
name='directory',
field=models.ForeignKey(default=lambda: temp_first_folder["id"], on_delete=django.db.models.deletion.CASCADE, related_name='files', to='app.directory'),
preserve_default=False,
),
# Run your code to set the actual default values
migrations.RunPython(
code=initial_folders,
reverse_code=migrations.RunPython.noop
),
]
You could avoid the temp_first_folder
variable by hardcoding the default=1
and therefore assuming that when you create the Directory it will be the very first row, but I prefer to set a variable because that will ensure that everything works
Happy coding
- [Django]-How can I upgrade specific packages using pip and a requirements file?
- [Django]-Create Django model or update if exists
- [Django]-ImportError: Failed to import test module:
-1
If the SSH it gives you 2 options, choose number 1, and put “None”. Just that…for the moment.
- [Django]-How to get the name of current app within a template?
- [Django]-Get user profile in django
- [Django]-Suddenly when running tests I get "TypeError: 'NoneType' object is not iterable
-2
If your table is new and you dont care about the data follow below steps:
Delete FROM public.django_migrations where app=<your_app_name>
Delete all migrations file from your project
DROP table <your_table_name>
python manage.py makemigrations
python manage.py migrate
If you care about data, then add a default to your field
new_field = models.CharField(max_length=140, default='your_default_value')
- [Django]-Sending HTML email in django
- [Django]-Get protocol + host name from URL
- [Django]-Django REST Framework: how to substitute null with empty string?