必须使用instance作为第一个参数调用unbound方法

时间:2014-04-19 11:09:56

标签: django

我正在使用django并且我收到了这个错误:“必须使用SocialUrl实例作为第一个参数调用未绑定方法create()(没有取而代之)”。我在这里已经阅读了几个相同问题的答案,但我不确定他们和我一样做错了。

这是包含我试图调用的方法的模型:

from django.db import models

class SocialUrl(models.Model):
code = models.CharField(max_length=30)

def create():
    socialUrl = SocialUrl(code = generateCode())
    socialUrl.save()
    return socialUrl


def __unicode__(self):
    return self.code

以下是尝试调用SocialUrl.create()的方法:

from django.shortcuts import render
from django.template import RequestContext
from .general import generateCode
from .models import SocialUrl

def test(request):

socialUrl = SocialUrl.create()
#print(SocialUrl.objects.all())
return render(request, 'test.html', RequestContext(request, {"title": socialUrl.code}))

这是stacktrace:

Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/su/asdadsf

Django Version: 1.6.2
Python Version: 2.7.6
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'SocialUrl')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware')


Traceback:
File "C:\Users\Sverker\.virtualenvs\coomba\lib\site-    packages\django\core\handlers\base.py" in get_response
114.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Sverker\Dropbox\Coomba\SbrgCoomba\SocialUrl\views.py" in test
8.  socialUrl = SocialUrl.create()

Exception Type: TypeError at /su/asdadsf
Exception Value: unbound method create() must be called with SocialUrl instance as first argument (got nothing instead)

帮助将不胜感激:)

1 个答案:

答案 0 :(得分:2)

在调用类上的方法之前,需要实例化该类。将您的观点更改为:

from django.shortcuts import render
from django.template import RequestContext
from .general import generateCode
from .models import SocialUrl

def test(request):

socialUrl = SocialUrl()
socialUrl.create()
#print(SocialUrl.objects.all())
return render(request, 'test.html', RequestContext(request, {"title": socialUrl.code}))

注意如何首先实例化对象(socialUrl = SocialUrl())然后在已实例化的对象上运行create。 (socialUrl.create())。

但是,我不确定您的创建方法是否会按预期运行。您想用create方法完成什么?

编辑以发表评论:

我不熟悉Java静态方法,但我认为你想要做的标准方法如下:

首先,按照您的要求定义模型。请注意,我删除了create方法。

from django.db import models

class SocialUrl(models.Model):
code = models.CharField(max_length=30)

def __unicode__(self):
    return self.code

然后,在您的视图中,实例化一个模型并使用您想要的属性填充它。

from django.shortcuts import render
from django.template import RequestContext
from .general import generateCode
from .models import SocialUrl

def test(request):

    my_code = generateCode()
    socialUrl = SocialUrl()
    socialUrl.code = my_code # Setting code attribute to the result of your generateCode function
    socialUrl.save() # Saves instance of SocialUrl to database.
    return render(request, 'test.html', RequestContext(request, {"title": socialUrl.code}))