psjinx's blog


So, you want to create users using django-tastypie

Building a RESTful api for user registration using django-tastypie. It includes a github project for Django 1.5 style custom User model along with explanation for each step.

So lately, I have been thinking to write about what I learnt in past 5-6 months. I will force myself to write something fun and useful every once in a while, mostly related to Django for next few weeks.

I worked with django-tastypie for last 2-3 months to implement RESTful API for iOS applications. It involved implementation of an end point for creating new users.

This is not something which is really documented, so here I'm sharing my experience. In a nutshell, we need to override obj_create method of ModelResource and do some validation on posted data. After few iterations I ended up with a code which is easily maintainable with future relases of django-tastypie.

tl;dr

  1. Check if all required fields are set in posted data.
  2. Check if there is an existing user with same username or email address.
  3. Validate posted data (e.g. password validation).
  4. Set password from raw password.
  5. Hide raw password from response.

Let's dive in.

Update

I have created an example project on Github for user registration. It uses Django 1.5, Custom user model and django-tastypie v0.10.0.

UserProfile model

django-tastypie==0.9.14 and Django 1.4.x have been used for this example. Since, support for custom User model was not very good in django-tastypie till v0.9.15, I had to define UserProfile model in Django 1.4.x style.

Here is the code for UserProfile model:

In case you need custom password validation

If you have some rules for passwords then it's best to have that logic in a function, because we may need to use that at different places (custom view for resetting password?).

I defined following validate_password function which uses regular expressions:

Raising custom exception for bad http requests

Format of JSON response for bad requests changed from v0.9.12 to v0.9.14 in django-tastypie which caused some issues for iOS application we were building. So, I wrote following custom class to solve this:

Model resources to manage user profiles

I have defined 3 model resources as you can see in following code snippet:

  1. UserResource - it uses contrib.auth.models.User.
  2. CreateUserResource - used for creating new UserProfile.
  3. UserProfileResource - used to GET/PUT/PATCH UserProfile objects.

I had to define two resources for UserProfile model because creating new user will be unauthenticated POST request but GET/PUT/PATCH need request.user for filtering. Currently there is no support for specifying authentication for each method method.

Following steps are involved in validating posted data:

  1. hydrate method in CreateUserResource is used to check if required fields are available in bundle.data or not.
  2. obj_create method in CreateUserResource is used to check if there is any other user with same email and username.
  3. hydrate method in UserResource is used to validate raw_password.
  4. dehydrate method in UserResource is used to delete raw_password from

You should also check if there is any other user with same username or email if your app has option to update username or email. It can be done by comparing bundle.obj.username with bundle.data.get('username') in hydrate method of UserResource.

Conclusion

This simple implementation is easiliy maintainable. If you have any questions or suggestions please post them in comments.

blog comments powered by Disqus

Follow Me





Subscribe To My Feed

While you're here, why not add my atom feed to your RSS reader or subscribe via email ?

Tags

django django-tastypie rest-api xlsxjs xlsx javascript excel workbook


Creative Commons License Using Jekyll, Pygments, Foundation | Sitemap