1

I need to add the required argument: on_delete = models.CASCADE to the following code. Otherwise, I get the following

TypeError: __init__() missing 1 required positional argument: 'on_delete'.

How and where should I do this?

class FilerFileField(models.ForeignKey):
    default_form_class = AdminFileFormField
    default_model_class = File 

    def __init__(self, **kwargs):
        # We hard-code the `to` argument for ForeignKey.__init__
        dfl = get_model_label(self.default_model_class)
        if "to" in kwargs.keys():  # pragma: no cover
            old_to = get_model_label(kwargs.pop("to"))
            if old_to != dfl:
                msg = "%s can only be a ForeignKey to %s; %s passed" % (
                    self.__class__.__name__, dfl, old_to
                )
                warnings.warn(msg, SyntaxWarning)
        kwargs['to'] = dfl
        super(FilerFileField, self).__init__(**kwargs)

    def formfield(self, **kwargs):
        # This is a fairly standard way to set up some defaults
        # while letting the caller override them.
        defaults = {
            'form_class': self.default_form_class,
        }
        try:
            defaults['rel'] = self.remote_field
        except AttributeError:
            defaults['rel'] = self.rel
        defaults.update(kwargs)
        return super(FilerFileField, self).formfield(**defaults)
pppery
  • 3,731
  • 22
  • 33
  • 46
user2901792
  • 677
  • 3
  • 7
  • 24

2 Answers2

2

It would be in the last line super(FilerFileField, self).__init__(**kwargs) of the __init__ function.

You can see that on_delete is a required parameter of ForeignKey class. https://docs.djangoproject.com/en/2.2/ref/models/fields/#foreignkey https://docs.djangoproject.com/en/2.2/ref/models/fields/#arguments

Something similar to super(FilerFileField, self).__init__(on_delete=models.CASCADE, **kwargs).

user12036762
  • 111
  • 3
  • @user2901792 Check the last line of the answer. You can replace that line with your current line. Along with models.CASCADE, there are multiple other values that you can set, according to your model integrity reqirements. These are explained in the second link. – user12036762 Sep 15 '19 at 00:03
  • @user2901792 Please mark it as answer if it is working for you. – user12036762 Sep 15 '19 at 00:06
0

When you use any Filer's models like FK you need to add on_delete=models.CASCADE