嗨,我是 django 和 python 的新手。我想扩展 djangoUser
模型并在模型中添加一个方法,然后我从视图中create_user
调用此方法。create_user
但是,我收到一条错误消息。
我的模型:
from django.db import models
from django.contrib.auth.models import User
class Basic(models.Model):
user = models.OneToOneField(User)
gender = models.CharField(max_length = 10)
def create_ck_user(acc_type, fb_id,fb_token):
# "Create a user and insert into auth_user table"
user = User.objects.create_user(acc_type,fb_id,fb_token)
user.save()
class External(models.Model):
user = models.OneToOneField(Basic)
external_id = models.IntegerField()
locale = models.CharField(max_length = 40)
token = models.CharField(max_length = 250)
鉴于,我做了这样的事情:
Basic.create_ck_user('acc_type' = 'fb', 'fb_id' = fb_id, 'fb_token' = fb_token)
错误表明:
keyword can't be an expression (views.py, Basic.objects.create_ck_user('acc_type' = 'fb', 'fb_id' = fb_id, 'fb_token' = fb_token))
编辑:添加@classmehtod 后,将view.py 更改为:
...
if (request.method == 'POST'):
acc_type = request.POST.get('acc_type')
fb_id = request.POST.get('fb_id')
fb_token = request.POST.get('fb_token')
Basic.create_ck_user(acc_type,fb_id,fb_token)
...
错误消息显示:
create_ck_user() 正好需要 3 个参数(给定 4 个)
检查错误详细信息,它包括“请求”变量,尽管我只传递了 3 个变量acc_type
:fb_id
和fb_token
.
有任何想法吗?