AlexW
AlexW

Reputation: 2587

Django - update or create obj must be an instance or subtype of type

I have an update or create method in my form valid function and im getting the error, when I submit the form. Im not sure as to why?

super(type, obj): obj must be an instance or subtype of type

Full trace:

File "/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
  41.             response = get_response(request)

File "/usr/local/lib/python3.6/site-packages/django/core/handlers/base.py" in _legacy_get_response
  249.             response = self._get_response(request)

File "/usr/local/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "/usr/local/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/usr/local/lib/python3.6/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
  23.                 return view_func(request, *args, **kwargs)

File "/itapp/itapp/config/views.py" in device_details
  151.         form = DeviceSubnetForm(request.POST)

File "/itapp/itapp/config/forms.py" in __init__
  135.         super(DeviceSubnet, self).__init__(*args, **kwargs)

Exception Type: TypeError at /config/device_details/2/7
Exception Value: super(type, obj): obj must be an instance or subtype of type

This is the function within the view

if request.method == 'GET':
    form = DeviceSubnetForm()
else:
    # A POST request: Handle Form Upload
    form = DeviceSubnetForm(request.POST)
    # If data is valid, proceeds to create a new post and redirect the user
    if form.is_valid():
        subnet_data = form.save(commit=False)
        obj, record = DeviceSubnet.objects.update_or_create(
            defaults={
                'subnet' : subnet_data.subnet,
                'subnet_mask'  : subnet_data.subnet_mask,
                'subnet_type' : SubnetTypes.objects.get(subnet_data.subnet_type)
                },
            subnet=subnet_data.subnet,
            subnet_mask=subnet_data.subnet_mask,
            subnet_type=SubnetTypes.objects.get(subnet_data.subnet_type)
        )
        print(obj.id)
        return 'Valid'

forms.py

class DeviceSubnetForm(forms.ModelForm):
    class Meta:
        model = DeviceSubnet
        fields = ['subnet', 'subnet_mask','subnet_type',]

    def __init__(self, *args, **kwargs):
        super(DeviceSubnet, self).__init__(*args, **kwargs)
        for visible in self.visible_fields():
            visible.field.widget.attrs['class'] = 'form-control'

Upvotes: 0

Views: 932

Answers (1)

neverwalkaloner
neverwalkaloner

Reputation: 47364

Since this code super(DeviceSubnet, self).__init__(*args, **kwargs) is located inside DeviceSubnetForm you should replace first argument of super method to DeviceSubnetForm class:

super(DeviceSubnetForm, self).__init__(*args, **kwargs)

or with python3 you skip arguments:

super().__init__(*args, **kwargs)

Upvotes: 5

Related Questions