B
B
bobs322019-12-09 03:44:54
Django
bobs32, 2019-12-09 03:44:54

Why does an argument error appear in a class?

When running the code:

class MenuItem(models.Model):
    menu = models.ForeignKey(
        Menu,
        verbose_name = _('menu'),
    )
    parent = models.ForeignKey(
        'self',
        verbose_name = _('parent'),
        related_name = u'child',
        blank = True,
        null = True,
    )
    name = models.CharField(
        max_length = 150,
        verbose_name = _('name'),
    )
    uri = models.SlugField(
        verbose_name = _('URL'),
        help_text = _('If you do not want to connect this item to your models, you can specify a URL explicitly'),
        blank = True,
        null = True,
    )
    visible = models.CharField(
        max_length = 1,
        verbose_name = _('visible'),
        choices = CHOICES_VISIBLE,
        default = CHOICES_VISIBLE[0][0],
    )
    css_class = models.CharField(
        max_length = 50,
        verbose_name = _('CSS class'),
        help_text = u'',
        blank = True,
        null = True,
    )
    
    class Meta:
        verbose_name = _('menu item')
        verbose_name_plural = _('menu items')
    
    def get_absolute_url(self):
        return '/%s' % self.uri
    
    def __unicode__(self):
        return self.name

An error:

class MenuItem(models.Model):
File "/Users/v/Documents/app1/mysite/menu/models.py", line 36, in MenuItem
verbose_name = _('menu'),
TypeError: __init__() missing 1 required positional argument: 'on_delete'

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrew, 2019-12-09
@bobs32

Because you need to read the documentation (v2.2) or v3.0
Example:

class MenuItem(models.Model):
    menu = models.ForeignKey(
        Menu,
        verbose_name = _('menu'),
        on_delete=models.CASCADE,
    )

D
Dr. Bacon, 2019-12-09
@bacon

Your code under python2 and old versions of django, on_delete has long been a required parameter https://docs.djangoproject.com/en/3.0/ref/models/f...
written.
ZY for such use of get_absolute_url beat on hands.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question