Provide default value for extra field in Django model

I wrote a Python model as shown below:

from django.db import models

class Product(models.Model):


        title = models.CharField(max_length=255, unique=True)
        description = models.TextField(blank=True)
        image_url = models.URLField(blank=True)
        quantity = models.PositiveIntegerField(default=0)

        def sell(self):

                self.quantity = self.quantity - 1
                self.save()
                return self.quantity

      

When I try to create a schema using migrate, I get the following message:

You are trying to add a non-nullable field 'description' to product 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: 

      

My question is, if I set 'blank = True' for 'description', do I need to provide a default value for the field? Or am I missing something else?

+3


source to share


2 answers


blank=True

is not the same as null=True

, as the documentation explains . When the text box is empty, it still needs some value: but that value can be an empty string.



So, just select option 1 and enter ''

as default.

+3


source


For Django 1.7, there is such a ticket created for this behavior. Take a look here



+2


source







All Articles