【问题标题】:Create a User Object with a Call to TastyPie API通过调用 TastyPie API 创建用户对象
【发布时间】:2013-12-02 21:52:03
【问题描述】:

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

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)

【问题讨论】:

  • 你的Customer 型号是什么?
  • 它继承自 django.contrib.auth.models.User。上面添加了

标签: python django tastypie


【解决方案1】:

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

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

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

【讨论】:

  • 我很困惑。为什么?我将模型管理器放在哪里?我该如何使用它?
  • @user1427661,我已经改写了答案,希望现在更清楚了。
  • 嗯。尽管传入了参数,但我的 **kwargs 参数似乎为空,导致当我尝试执行上述覆盖时出现关键错误。
  • 试试bundle.data字典。
猜你喜欢
  • 2012-08-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-21
  • 1970-01-01
  • 1970-01-01
  • 2013-07-16
相关资源
最近更新 更多