Python error with Django on_delete

Asked

Viewed 196 times

0

I’m a beginner in the Django platform and the Python language and I’m developing a website in Django 2.2 and Python 3.7.3, and I have a problem with my code on models.py from my app "Courses".

class Enrollment(models.Model):

    STATUS_CHOICES = (
        (0, 'Pendente'),
        (1, 'Aprovado'),
        (2, 'Cancelado'),
    )

    user = models.ForeignKey(
        settings.AUTH_USER_MODEL, verbose_name='Usuário',
        models.CASCADE,
        related_name='enrollments'
    )
    course = models.ForeignKey(
        Course, verbose_name='Curso', related_name='enrollments'
    )
    status = models.IntegerField(
        'Situação', choices=STATUS_CHOICES, default=1, blank=True
    )

    created_at = models.DateTimeField('Criado em', auto_now_add=True)
    updated_at = models.DateTimeField('Atualizado em', auto_now=True)

    def active(self):
        self.status = 1
        self.save()

    class Meta:
        verbose_name = 'Inscrição'
        verbose_name_plural = 'Inscrições'
        unique_together = (('user', 'course'),)

And you’re making that mistake:

Traceback (most recent call last):
  File "manage.py", line 21, in <module>
    main()
  File "manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "C:\Users\Luciano\ambientes_virtuais\ambientes_virtuais\virtual_01\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "C:\Users\Luciano\ambientes_virtuais\ambientes_virtuais\virtual_01\lib\site-packages\django\core\management\__init__.py", line 357, in execute
    django.setup()
  File "C:\Users\Luciano\ambientes_virtuais\ambientes_virtuais\virtual_01\lib\site-packages\django\__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "C:\Users\Luciano\ambientes_virtuais\ambientes_virtuais\virtual_01\lib\site-packages\django\apps\registry.py", line 114, in populate
    app_config.import_models()
  File "C:\Users\Luciano\ambientes_virtuais\ambientes_virtuais\virtual_01\lib\site-packages\django\apps\config.py", line 211, in import_models
    self.models_module = import_module(models_module_name)
  File "C:\Users\Luciano\ambientes_virtuais\ambientes_virtuais\virtual_01\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\Luciano\ambientes_virtuais\ConnectU\ConnectU\courses\models.py", line 53, in <module>
    class Enrollment(models.Model):
  File "C:\Users\Luciano\ambientes_virtuais\ConnectU\ConnectU\courses\models.py", line 67, in Enrollment
    Course, verbose_name='Curso', related_name='enrollments'
TypeError: __init__() missing 1 required positional argument: 'on_delete'

What can I do?

1 answer

1

Felipe, if you check the field documentation Model.ForeignKey you will see that the method signature is:

ForeignKey(to, on_delete, **options)

And by checking the source code we have the builder:

def __init__(self, to, on_delete, related_name=None, related_query_name=None,
             limit_choices_to=None, parent_link=False, to_field=None,
             db_constraint=True, **kwargs):
    ...

This means that there are two mandatory parameters, to and on_delete, the rest is optional. In your model you make:

user = models.ForeignKey(
    settings.AUTH_USER_MODEL, 
    verbose_name='Usuário',
    models.CASCADE,
    related_name='enrollments'
)
course = models.ForeignKey(
    Course, verbose_name='Curso', related_name='enrollments'
)

If you notice that in user you are calling models.ForeignKey using:

  1. A positional argument with the value settings.AUTH_USER_MODEL
  2. A named argument verbose_name with the value "Usuário"
  3. A positional argument with the value models.CASCADE
  4. A named argument related_name with the value "enrollments"

You cannot cross-reference named and positional arguments when calling a function. You must pass the positional arguments before the nominees. And as shown in the source code and documentation the arguments are to and on_delete, then you need to change your code so that these parameters are passed correctly:

  1. Passing the parameter on_delete right after the parameter to:

    user = models.ForeignKey(
        settings.AUTH_USER_MODEL, 
        models.CASCADE,
        verbose_name='Usuário',
        related_name='enrollments'
    )
    
  2. Passing the parameter on_delete in naming him:

    user = models.ForeignKey(
        settings.AUTH_USER_MODEL, 
        verbose_name='Usuário',
        on_delete=models.CASCADE,
        related_name='enrollments'
    )
    

Browser other questions tagged

You are not signed in. Login or sign up in order to post.