通过调用TastyPie API创建用户对象

时间:2013-12-02 21:52:03

标签: python django tastypie

我有一个Tastypie资源,用于从django.contribut.auth的用户模型扩展的模型(只是几个额外的字段)。这是资源代码:

class CustomerResource(ModelResource):

    locations = fields.ToManyField('device.resources.LocationResource',
            'location_set', null=True)
    current_location = fields.ToOneField('device.resources.LocationResource',
            'current_location', null=True)
    default_location = fields.ToOneField('device.resources.LocationResource',
            'default_location', null=True)

    class Meta:
        queryset = Customer.objects.all()
        resource_name = 'customers'
        validation = CleanedDataFormValidation(form_class=RegistrationForm)
        list_allowed_methods = ['get', 'post']
        detail_allowed_methods = ['get', 'put', 'patch', 'delete']
        authorization = Authorization()
        excludes =['is_superuser', 'is_active', 'is_staff', 'password', 'last_login',]
        filtering = {
            'location': ('exact'),
        }

    def obj_create(self, bundle, **kwargs):
        bundle.data['username'] = bundle.data['email']
        return super(CustomerResource, self).obj_create(bundle, **kwargs)

我希望能够使用从表单收集的一些JSON(它不仅仅是表单数据,因为我需要进行一些处理)来创建用户,从而对API进行POST。这是我发布的API的Django代码:

    payload = {}
    payload['email'] = request.POST['username']
    payload['username'] = request.POST['username']
    payload['password1'] = request.POST['password1']
    payload['password2'] = request.POST['password2']
    payload = json.dumps(payload)

    customer = requests.post(self.base_url + '/v1/customers/', data=payload, headers=headers)

这可以很好地发布客户 - 它存在于API的数据库中。但是,由于某种原因,其密码未注册。密码属性为空,而如果我在API的数据库中本地创建用户,则密码显示为加密的哈希。 has_usable_password字段设置为false。如果我尝试payload['password'] = request.POST['password1'],也会发生同样的事情。有什么建议?

我的客户模型:

class Customer(AbstractUser):

    current_location = models.ForeignKey('device.Location',
            null=True, blank=True, related_name='customers_present')
    default_location = models.ForeignKey('device.Location',
            null=True, blank=True, related_name='default_customers')

    def __unicode__(self):
        return u'{0}'.format(self.username)

1 个答案:

答案 0 :(得分:1)

您的Customer模型继承了名为UserManager的自定义objects经理属性。它允许您轻松创建Customer个实例并负责密码加密。

您必须覆盖obj_create中的CustomerResource方法:

class CustomerResource(ModelResource):

    locations = fields.ToManyField('device.resources.LocationResource',
            'location_set', null=True)
    current_location = fields.ToOneField('device.resources.LocationResource',
            'current_location', null=True)
    default_location = fields.ToOneField('device.resources.LocationResource',
            'default_location', null=True)

    class Meta:
        queryset = Customer.objects.all()
        resource_name = 'customers'
        validation = CleanedDataFormValidation(form_class=RegistrationForm)
        list_allowed_methods = ['get', 'post']
        detail_allowed_methods = ['get', 'put', 'patch', 'delete']
        authorization = Authorization()
        excludes =['is_superuser', 'is_active', 'is_staff', 'password', 'last_login',]
        filtering = {
            'location': ('exact'),
        }

    def obj_create(self, bundle, **kwargs):
        bundle.obj = self._meta.object_class.objects.create_user(
            username=kwargs['username'],
            email=kwargs['email'],
            password=kwargs['password1'],
        )
        return bundle