Python 通过调用tastypieapi创建用户对象
我有一个从django.contribut.auth的用户模型扩展而来的模型的Tastypie资源(只是几个额外的字段)。以下是资源代码:Python 通过调用tastypieapi创建用户对象,python,django,tastypie,Python,Django,Tastypie,我有一个从django.contribut.auth的用户模型扩展而来的模型的Tastypie资源(只是几个额外的字段)。以下是资源代码: class CustomerResource(ModelResource): locations = fields.ToManyField('device.resources.LocationResource', 'location_set', null=True) current_location = fields
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发布帖子,以创建用户。下面是我发布到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的数据库中。但是,由于某些原因,其密码未注册。password属性为空,而如果在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)
您的
客户
模型继承了名为对象
的自定义用户管理器
管理器属性。它允许您轻松创建客户
实例,并负责密码加密
您必须在CustomerResource
中重写obj\u 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
您的
客户模型是什么?它继承自django.contrib.auth.models.User。我很困惑。为什么?我把模型管理器放在哪里?我该如何使用它?@user1427661,我已经重新表述了答案,希望现在更清楚了。嗯。尽管传入了参数,但我的**kwargs参数似乎是空的,这导致我在尝试执行上面描述的覆盖时出现关键错误。请尝试使用bundle.data
dictionary。