Fix Python – You are trying to add a non-nullable field ‘new_field’ to userprofile without a default

Question

Asked By – Irmantas Želionis

I know that from Django 1.7 I don’t need to use South or any other migration system, so I am just using simple command python manage.py makemigrations

However, all I get is this error:

You are trying to add a non-nullable field 'new_field' to userprofile without a default;
we can't do that (the database needs something to populate existing rows).

Here is models.py:

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    website = models.URLField(blank=True)
    new_field = models.CharField(max_length=140)

What are options?

Now we will see solution for issue: You are trying to add a non-nullable field ‘new_field’ to userprofile without a default


Answer

You need to provide a default value:

new_field = models.CharField(max_length=140, default='SOME STRING')

This question is answered By – dgel

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0