Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django-更新或创建obj必须是类型为的实例或子类型_Python_Django - Fatal编程技术网

Python Django-更新或创建obj必须是类型为的实例或子类型

Python Django-更新或创建obj必须是类型为的实例或子类型,python,django,Python,Django,我的表单有效函数中有一个update或create方法,当我提交表单时,我收到了错误。我不知道为什么 super(type, obj): obj must be an instance or subtype of type 完整跟踪: File "/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner 41. response = get_response(

我的表单有效函数中有一个update或create方法,当我提交表单时,我收到了错误。我不知道为什么

super(type, obj): obj must be an instance or subtype of type
完整跟踪:

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
这是视图中的函数

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'

由于此代码
super(DeviceSubnet,self)。\uuu init\uu(*args,**kwargs)
位于
DeviceSubnetForm
中,您应该将
super
方法的第一个参数替换为
DeviceSubnetForm
类:

super(DeviceSubnetForm, self).__init__(*args, **kwargs)
或者使用python3跳过参数:

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

由于此代码
super(DeviceSubnet,self)。\uuu init\uu(*args,**kwargs)
位于
DeviceSubnetForm
中,您应该将
super
方法的第一个参数替换为
DeviceSubnetForm
类:

super(DeviceSubnetForm, self).__init__(*args, **kwargs)
或者使用python3跳过参数:

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

我不知道你的意思,这是什么意思?@AlexW在你的forms.py文件中你有一行
super(DeviceSubnet,self)。\uuu init\uuuuuuu(*args,**kwargs)
你应该用
super()替换它。如果你使用的是python3或
super(classthiscodelocated,self)。\uu init
对于python 2.py,我在问题中添加了我的forms.py,我已经有了super,需要修改吗?谢谢她告诉你你有
super(DeviceSubnet,…)
而不是
super(DeviceSubnetForm,…)
@AlexW你必须用
super(DeviceSubnetForm,self)
替换
super(DeviceSubnetForm,self)
。我不确定你的意思,这是什么意思?@AlexW在你的forms.py文件中你有一行
super(DeviceSubnet,self)。\uuuu init\uuu(*args,**kwargs)
您应该将其替换为
super()
对于Python2.I我在问题中添加了我的forms.py,我已经有了super,需要修改吗?谢谢她告诉你你有
super(DeviceSubnet,…)
而不是
super(DeviceSubnetForm,…)
@AlexW你必须用
super(DeviceSubnet,self)
替换
super(DeviceSubnet,self)
(DeviceSubnetForm,self)