from django import forms

from .models import Clipboard


class ClipboardFileForm(forms.ModelForm):
    class Meta:
        model = Clipboard
        fields = ('file',)

    def save(self, commit=True):
        # Delete old file before saving the new one
        if self.instance.pk:
            old_instance = self._meta.model.objects.get(pk=self.instance.pk)
            old_instance.file.delete(save=False)
        return super(ClipboardFileForm, self).save(commit=commit)


class ClipboardImageForm(ClipboardFileForm):
    file = forms.ImageField()

from django import forms

from .models import Clipboard


class BaseClipboardForm(object):
    def save(self, commit=True):
        # Delete old file before saving the new one
        if self.instance.pk:
            old_instance = self._meta.model.objects.get(pk=self.instance.pk)
            old_instance.file.delete(save=False)
        return super(BaseClipboardForm, self).save(commit=commit)


class ClipboardFileForm(BaseClipboardForm, forms.ModelForm):
    class Meta:
        model = Clipboard
        fields = ('file',)


class ClipboardImageForm(ClipboardFileForm):
    file = forms.ImageField()

Create BaseClipboardForm for easy extending if needed
Create BaseClipboardForm for easy extending if needed
  from django import forms
  
  from .models import Clipboard
  
  
+ class BaseClipboardForm(object):
- class ClipboardFileForm(forms.ModelForm):
-     class Meta:
-         model = Clipboard
-         fields = ('file',)
- 
      def save(self, commit=True):
          # Delete old file before saving the new one
          if self.instance.pk:
              old_instance = self._meta.model.objects.get(pk=self.instance.pk)
              old_instance.file.delete(save=False)
-         return super(ClipboardFileForm, self).save(commit=commit)
+         return super(BaseClipboardForm, self).save(commit=commit)
+ 
+ 
+ class ClipboardFileForm(BaseClipboardForm, forms.ModelForm):
+     class Meta:
+         model = Clipboard
+         fields = ('file',)
  
  
  class ClipboardImageForm(ClipboardFileForm):
      file = forms.ImageField()
  
Create BaseClipboardForm for easy extending if needed
## Code Before:
from django import forms

from .models import Clipboard


class ClipboardFileForm(forms.ModelForm):
    class Meta:
        model = Clipboard
        fields = ('file',)

    def save(self, commit=True):
        # Delete old file before saving the new one
        if self.instance.pk:
            old_instance = self._meta.model.objects.get(pk=self.instance.pk)
            old_instance.file.delete(save=False)
        return super(ClipboardFileForm, self).save(commit=commit)


class ClipboardImageForm(ClipboardFileForm):
    file = forms.ImageField()

## Instruction:
Create BaseClipboardForm for easy extending if needed
## Code After:
from django import forms

from .models import Clipboard


class BaseClipboardForm(object):
    def save(self, commit=True):
        # Delete old file before saving the new one
        if self.instance.pk:
            old_instance = self._meta.model.objects.get(pk=self.instance.pk)
            old_instance.file.delete(save=False)
        return super(BaseClipboardForm, self).save(commit=commit)


class ClipboardFileForm(BaseClipboardForm, forms.ModelForm):
    class Meta:
        model = Clipboard
        fields = ('file',)


class ClipboardImageForm(ClipboardFileForm):
    file = forms.ImageField()
