{"id":7775,"date":"2015-10-25T01:41:30","date_gmt":"2015-10-25T01:41:30","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2015\/10\/25\/how-to-register-users-in-django-rest-framework-open-source-projects-tomchristie-django-rest-framework\/"},"modified":"2015-10-25T01:41:30","modified_gmt":"2015-10-25T01:41:30","slug":"how-to-register-users-in-django-rest-framework-open-source-projects-tomchristie-django-rest-framework","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2015\/10\/25\/how-to-register-users-in-django-rest-framework-open-source-projects-tomchristie-django-rest-framework\/","title":{"rendered":"how to register users in django-rest-framework?-open source projects tomchristie\/django-rest-framework"},"content":{"rendered":"<p><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/43a45220bbf73fdd059ab9278cbcdf4a?s=128&amp;d=identicon&amp;r=PG\" \/> <strong>cpury<\/strong><\/p>\n<p>The simplest solution, working in DRF 3.x:<\/p>\n<pre><code>class UserSerializer(serializers.ModelSerializer):\n    class Meta:\n        model = User\n        fields = ('id', 'username', 'password', 'email', 'first_name', 'last_name')\n        write_only_fields = ('password',)\n        read_only_fields = ('id',)\n\n    def create(self, validated_data):\n        user = User.objects.create(\n            username=validated_data['username'],\n            email=validated_data['email'],\n            first_name=validated_data['first_name'],\n            last_name=validated_data['last_name']\n        )\n\n        user.set_password(validated_data['password'])\n        user.save()\n\n        return user\n<\/code><\/pre>\n<p>No need for other changes, just make sure that unauthenticated users have the permission to create a new user object.<\/p>\n<p><code>write_only_fields<\/code> will make sure passwords (actually: their hash we store) are not displayed, while the overwritten <code>create<\/code> method ensures that the password is not stored in clear text, but as a hash.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>cpury The simplest solution, working in DRF 3.x: class UserSerializer(serializers.ModelSerializer): class Meta: model = User fields = (&#8216;id&#8217;, &#8216;username&#8217;, &#8216;password&#8217;, &#8217;email&#8217;, &#8216;first_name&#8217;, &#8216;last_name&#8217;) write_only_fields = (&#8216;password&#8217;,) read_only_fields = (&#8216;id&#8217;,) def create(self, validated_data): user = User.objects.create( username=validated_data[&#8216;username&#8217;], email=validated_data[&#8217;email&#8217;], first_name=validated_data[&#8216;first_name&#8217;], last_name=validated_data[&#8216;last_name&#8217;] ) user.set_password(validated_data[&#8216;password&#8217;]) user.save() return user No need for other changes, just make sure that unauthenticated users [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-7775","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7775","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=7775"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7775\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=7775"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=7775"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=7775"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}