0

guys i'm getting an error as you should have a default value to models:author,body,created,updated and here is my models.py configuration

from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
# Create your models here.
class post(models.Model):
    STATUS_CHOICE=(
        ('draft','DRAFT'),
        ('published','Published'),
    )

    title=models.CharField(max_length=250)
    slug=models.SlugField(max_length = 250,unique_for_date=1)
    author=models.ForeignKey(User,related_name='blog_posts')
    body=models.TextField()
    publish=models.DateTimeField(default=timezone.now)
    created = models.DateTimeField(auto_now_add=True)
    updated=models.DateTimeField(auto_now=True)
    status = models.CharField(max_length=10,
                            choices = STATUS_CHOICE,
                                default='draft')
    class Meta:
        ordering = ('-publish',)
    def __str__(self):
        return self.title

and this is the error while migrating the database:

You are trying to add a non-nullable field 'body' to post 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 with a null value for this column)
 2) Quit, and let me add a default in models.py
Select an option

i'm not sure what are the default values for those models and the configuration is same as it is mentioned in book but i'm still getting an error any kind of help is appreciated thanks in advance NOTE:this is a blog application and when i tried to add random default values to the models i'm getting this error: updated models.py:

from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
# Create your models here.
class post(models.Model):
    STATUS_CHOICE=(
        ('draft','DRAFT'),
        ('published','Published'),
        ('admin','admin'),
    )

    title=models.CharField(max_length=250)
    slug=models.SlugField(max_length = 250)
    author=models.ForeignKey(User,related_name='blog_posts')
    body=models.TextField(default='draft')
    publish=models.DateTimeField(default=timezone.now)
    created = models.DateTimeField(auto_now_add=True,default=timezone.now)
    updated =models.DateTimeField(auto_now=True,default=timezone.now)
    status = models.CharField(max_length=10,
                            choices = STATUS_CHOICE,
                                default='draft')
    class Meta:
        ordering = ('-publish',)
    def __str__(self):
        return self.title

and the error now is :

ERRORS:
myblog.post.created: (fields.E160) The options auto_now, auto_now_add, and default are mutually exclusive. Only one of these options may
 be present.
myblog.post.updated: (fields.E160) The options auto_now, auto_now_add, and default are mutually exclusive. Only one of these options may
 be present
2
  • 1
    error looks pretty clear, you don't need both options in the DateTimeField Commented Oct 22, 2017 at 10:55
  • thanks for your reply yes i have removed the value for both DateTimeField and added default values but stiill its asking me for slug and author as well please can you figure it out what exactly the default values should be Commented Oct 22, 2017 at 11:04

1 Answer 1

1

Why don't you use null=True, blank=True - that allows for model attributes to be empty!

I would set the default of the body attribute to ''

body=models.TextField(default='')

The timestamps should not have a default, the callables you provide should be enough.

# Model Timestamp
created     = models.DateTimeField(auto_now_add=True)
updated     = models.DateTimeField(auto_now=True)

When doing the migration and it runs into a missing default error for the created and updated attributes, select 1) and enter in the console timezone.now(). That sets the default for existing rows to the current datetime.

Sign up to request clarification or add additional context in comments.

3 Comments

thanks for having a look at my issue after editing everything as you said i'm still getting this error: You are trying to add the field 'created' with 'auto_now_add=True' to post without a default; the database needs something to populate e xisting rows. 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: myblog\models.py14:71
I changed the answer, now it should go through.
thanks it worked i just added null = true for all those ,and if you want to help and solve issues for beginnes like me please join in this slack team :join.slack.com/t/python-django-adroits/shared_invite/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.