0

Please help out

I am designing a blog app using django 2.1.4. Actually, I am following the instructions from the book "Django by example" by antonio mele which uses django 1.8. However, after creating the app and adding the blog model, I tried applying the migration for the new model and I get this error message.

Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Users\public\django\my_env\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_c
ommand_line
    utility.execute()
  File "C:\Users\public\django\my_env\lib\site-packages\django\core\management\__init__.py", line 357, in execute
    django.setup()
  File "C:\Users\public\django\my_env\lib\site-packages\django\__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "C:\Users\public\django\my_env\lib\site-packages\django\apps\registry.py", line 112, in populate
    app_config.import_models()
  File "C:\Users\public\django\my_env\lib\site-packages\django\apps\config.py", line 198, in import_models
    self.models_module = import_module(models_module_name)
  File "C:\Users\public\django\my_env\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "C:\Users\public\django\mysite\blog\models.py", line 14, in <module>
    related_name='blog_posts')
TypeError: __init__() missing 1 required positional argument: 'on_delete'

@ Emmanuel.. this is the complete code on the models.py file

from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User

class Post(models.Model):
    STATUS_CHOICES = (
        ('draft', 'Draft'),
        ('published', 'Published'),
)
title = models.CharField(max_length=250)
slug = models.SlugField(max_length=250,
                        unique_for_date='publish')
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_CHOICES,
                        default='draft')
class Meta:
    ordering = ('-publish',)

def __str__(self):
    return self.title
  • It looks like something is wrong with your mode, so this is more a problem with *what* you want to migrate, instead of just the migration process itself. Can you share the relevant models? – Willem Van Onsem Jan 04 '19 at 12:03
  • Please, paste the command giving that error in the question – Evhz Jan 04 '19 at 12:04
  • Please, add some code to review it. https://stackoverflow.com/help/how-to-ask – Emmanuel Arias Jan 04 '19 at 12:07
  • @ willem class Post(models.Model): STATUS_CHOICES = ( ('draft', 'Draft'), ('published', 'Published'), ) title = models.CharField(max_length=250) slug = models.SlugField(max_length=250, unique_for_date='publish') 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_CHOICES, default='draft') class Meta: ordering = ('-publish',) – Chardonnay St. Patrick Jan 04 '19 at 12:08
  • @Evhz.. python manage.py makemigrations blog – Chardonnay St. Patrick Jan 04 '19 at 12:09
  • @ Emmanuel i have edited the post to include the code you require. Thanks. – Chardonnay St. Patrick Jan 04 '19 at 12:20
  • The error message is not complete it lacks the most important part ... – shredding Jan 04 '19 at 13:02
  • @shredding i have updated the post to contain the complete error message. thanks – Chardonnay St. Patrick Jan 04 '19 at 13:15
  • I dont understand why nobody is simply answering your question... A Class with a ForeignKey (in your case author connected to User) needs an attribute "on_delete" which is required on all foreignkeys, this question was answered multiple times (https://stackoverflow.com/questions/38388423/what-does-on-delete-do-on-django-models). – hansTheFranz Jan 04 '19 at 13:26
  • 1
    I think If I remember properly from django 2.0 onwards we should use on_delete for foreign key relations. Try that for author field. – Nagashayan Jan 04 '19 at 13:26
  • 1
    @hansTheFranz: Because the error message was missing when we where looking. – shredding Jan 04 '19 at 14:45
  • the code is fine now.. : ). thanks to everyone that contributed. You guys rock!!! – Chardonnay St. Patrick Jan 04 '19 at 16:23

0 Answers0