我正在尝试使用tastepie实现用户注册,一切正常,但是当我尝试使用obj_create方法检查用户是否已经存在时,什么都没有发生......如何在tastepie中调用它,它会自动调用吗?
from _mysql_exceptions import IntegrityError
from django.db.models.query_utils import Q
from tastypie.authorization import Authorization
from django.contrib.auth.models import User
from tastypie.authentication import BasicAuthentication
from tastypie import fields
from tastypie.exceptions import BadRequest
from tastypie.resources import ModelResource, ALL, ALL_WITH_RELATIONS
from tastypie.validation import Validation
from apps.eUser.models import UserProfile
class ProfileResource(ModelResource):
username = fields.CharField(attribute='user__username', readonly=True)
id = fields.CharField(attribute='id')
class Meta:
queryset =UserProfile.objects.select_related('User')
resource_name = 'profile'
fields = ['id']
class UserResource(ModelResource):
class Meta:
object_class = User
queryset = User.objects.all()
resource_name = 'user'
allowed_methods = ['get', 'post', 'delete', 'put']
#excludes = ['email', 'password', 'is_active', 'is_staff', 'is_superuser']
filtering = {
}
class CreateUserResource(ModelResource):
class Meta:
allowed_methods = ['post']
object_class = User
#authentication = Authentication()
authorization = Authorization()
resource_name = 'register'
include_resource_uri = False
fields = ['username','password','email']
def obj_create(self, bundle, request=None, **kwargs):
try:
bundle = super(CreateUserResource, self).obj_create(bundle, request, **kwargs)
except IntegrityError:
raise BadRequest('That username already exists')
return bundle