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
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)
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.
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.
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)
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)
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!