I am using django-podcasting in a django project [2.0.8] project.

When I run python manage.py makemigrations, I get the following stacktrace:

(site-env) [email protected]:~/Projects/dev/mysite$ python base_app/manage.py makemigrations                                              
Traceback (most recent call last):                                                                                                            
  File "base_app/manage.py", line 15, in <module>                                                                                             
    execute_from_command_line(sys.argv)                                                                                                       
  File "/path/to/proj/env/lib/python3.6/site-packages/django/core/management/__init__.py", line 371, in execute_from_comman
d_line                                                                                                                                        
    utility.execute()                                                                                                                         
  File "/path/to/proj/env/lib/python3.6/site-packages/django/core/management/__init__.py", line 347, in execute            
    django.setup()                                                                                                                            
  File "/path/to/proj/env/lib/python3.6/site-packages/django/__init__.py", line 24, in setup                               
    apps.populate(settings.INSTALLED_APPS)                                                                                                    
  File "/path/to/proj/env/lib/python3.6/site-packages/django/apps/registry.py", line 112, in populate                      
    app_config.import_models()
  File "/path/to/proj/env/lib/python3.6/site-packages/django/apps/config.py", line 198, in import_models
    self.models_module = import_module(models_module_name)
  File "/path/to/proj/env/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/path/to/proj/env/lib/python3.6/site-packages/podcasting/models.py", line 103, in <module>
    class Show(models.Model):
  File "/path/to/proj/env/lib/python3.6/site-packages/podcasting/models.py", line 128, in Show
    help_text=_("""Make certain the user account has a name and e-mail address."""))
TypeError: __init__() missing 1 required positional argument: 'on_delete'

1 Answers

0
Ben Boyer On Best Solutions

In new version of Django, on_delete is mandatory for ForeignKey and OneToOneField.

You have to define it in you model like:

class Car(models.Model):
    manufacturer = models.ForeignKey(
        'Manufacturer',
        on_delete=models.CASCADE,
    )

Here are the different possibilities for on_delete from the doc: https://docs.djangoproject.com/en/2.2/ref/models/fields/#django.db.models.ForeignKey.on_delete

on_delete doesn’t create a SQL constraint in the database. Support for database-level cascade options may be implemented later.

The possible values for on_delete are found in django.db.models:

CASCADE

Cascade deletes. Django emulates the behavior of the SQL constraint ON DELETE CASCADE and also deletes the object containing the ForeignKey.

Model.delete() isn’t called on related models, but the pre_delete and post_delete signals are sent for all deleted objects.

PROTECT

Prevent deletion of the referenced object by raising ProtectedError, a subclass of django.db.IntegrityError.

SET_NULL

Set the ForeignKey null; this is only possible if null is True.

SET_DEFAULT

Set the ForeignKey to its default value; a default for the ForeignKey must be set.

SET()

Set the ForeignKey to the value passed to SET(), or if a callable is passed in, the result of calling it. In most cases, passing a callable will be necessary to avoid executing queries at the time your models.py is imported:

 from django.conf import settings
    from django.contrib.auth import get_user_model
    from django.db import models

    def get_sentinel_user():
        return get_user_model().objects.get_or_create(username='deleted')[0]

    class MyModel(models.Model):
        user = models.ForeignKey(
            settings.AUTH_USER_MODEL,
            on_delete=models.SET(get_sentinel_user),
        )
DO_NOTHING

    Take no action. If your database backend enforces referential integrity, this will cause an IntegrityError unless you manually add an SQL ON DELETE constraint to the database field.