how to create a user and user profile in django rest framework

Solutions on MaxInterview for how to create a user and user profile in django rest framework by the best coders in the world

showing results for - "how to create a user and user profile in django rest framework"
Jona
04 Apr 2017
1class UserSerializer(serializers.ModelSerializer):
2    profile = ProfileSerializer(required=True)
3    class Meta:
4        model = User
5        fields = ('url', 'email', 'profile', 'created',)
6
7    def create(self, validated_data):
8
9        # create user 
10        user = User.objects.create(
11            url = validated_data['url'],
12            email = validated_data['email'],
13            # etc ...
14        )
15
16        profile_data = validated_data.pop('profile')
17        # create profile
18        profile = Profile.objects.create(
19            user = user
20            first_name = profile_data['first_name'],
21            last_name = profile_data['last_name'],
22            # etc...
23        )
24
25        return user
Lilly
27 Apr 2020
1class UserList(generics.ListCreateAPIView):
2    permission_classes = (IsAuthenticatedOrWriteOnly,)
3    serializer_class = UserSerializer
4
5    def post(self, request, format=None):
6        serializer = UserSerializer(data=request.data)
7        if serializer.is_valid():
8            serializer.save()
9            return Response(serializer.data, status=status.HTTP_201_CREATED)
10        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Tao
31 Apr 2019
1class UserManager(BaseUserManager):
2    def create_user(self, email, password=None):
3        if not email:
4            raise ValueError('User must have an email address')
5
6        user = self.model(
7            email = self.normalize_email(email),
8        )
9        user.set_password(password)
10        user.save()
11        return user
12
13    def create_superuser(self, email, password):
14        user = self.create_user(email, password=password)
15        user.is_admin = True
16        user.save()
17        return user
18
19
20class User(AbstractBaseUser):
21    objects = UserManager()
22    email = models.EmailField(unique=True, db_index=True)
23    created = models.DateTimeField('created', auto_now_add=True)
24    modified = models.DateTimeField(auto_now=True)
25
26    is_active = models.BooleanField('active', default=True)
27    is_admin = models.BooleanField('admin', default=False)
28
29    USERNAME_FIELD = 'email'
30
31    ordering = ('created',)
32
33    def is_staff(self):
34        return self.is_admin
35
36    def has_perm(self, perm, obj=None):
37        return True
38
39    def has_module_perms(self, app_label):
40        return True
41
42    def get_short_name(self):
43        return self.email
44
45    def get_full_name(self):
46        return self.email
47
48    def __unicode__(self):
49        return self.email
50
51
52class Profile(models.Model):
53    GENDER = (
54        ('M', 'Homme'),
55        ('F', 'Femme'),
56    )
57
58    user = models.OneToOneField(settings.AUTH_USER_MODEL)
59    first_name = models.CharField(max_length=120, blank=False)
60    last_name = models.CharField(max_length=120, blank=False)
61    gender = models.CharField(max_length=1, choices=GENDER)
62    zip_code = models.CharField(max_length=5, validators=[MinLengthValidator(5)], blank=False)
63
64    def __unicode__(self):
65        return u'Profile of user: {0}'.format(self.user.email)
66
67
68def create_profile(sender, instance, created, **kwargs):
69    if created:
70        Profile.objects.create(user=instance)
71post_save.connect(create_profile, sender=User)
72
73
74def delete_user(sender, instance=None, **kwargs):
75    try:
76        instance.user
77    except User.DoesNotExist:
78        pass
79    else:
80        instance.user.delete()
81post_delete.connect(delete_user, sender=Profile)
queries leading to this page
user profile in djangocreate profile with user model same instance rest frameworkdjango automatically add current user to model rest frameworkhow to create user in django rest frameworkdjango rest framework update user profilecustom user login system tutorial django restdjango rest view profilecreating profile api djangoresthow to update a user and user profile in django rest frameworkdjango create user from view rest apicreate a profile as user is created django restcreate custom user model django rest frameworkupdate user and user profile django rest frameworkcustom user login system django rest tutorialdjango rest api create userdjango rest framework api get user profiledjango rest framework custom user model registration and logindjango rest framework custom userhow to create user profile with rest frameworkdjango rest framework create user commandcreate user api in djangodjango rest framework profile user profile required django decoratorapi django rest get userhow to create user registration and login in django rest framework django restframework manage user in restapiget user data django rest frameworkdjango rest framework register user tutorialcustom user model django rest framework djoserdjango rest framework create with logged useradd a profile on user creation rest djangoregister user django rest frameworkdjango rest create useruser profile djangorestdjango rest framework user profilrdjango rest auth custom user modeldjango rest framework user registration and login apidjango rest framework custom user registrationdjango rest add a useruser sign up django rest apicustom user model in django rest frameworkcustom user model django rest framework mediumdjango rest framework register useruser registration in django rest frameworkdjango rest framework register new userdjango rest framework admin create userdjango rest framework create user api e2 80 9chow to update a user and user profile in django rest framework e2 80 9duser registration andlogin django rest frameworkdjango rest set user automaticallyuser profile django rest frameworkdjango rest framework add userdjango rest framework register user and updatehow to authenticate user in django rest frameworkcreate user django rest frameworkpython django create user profiledjango rest framework user registration examplerest registration profile djangodjango user profile apidjango custom user model django rest framework project tutorial 5b4 5dcustom user model rest djangobest way to create user authentication in django rest frameworkdjango rest api get usercreate company when user is created django rest frameworkcreate user authentication api with django rest frameworkuser accounts with django resteasy django rest framework user registration and logindjango rest get userdjango rest framework tutorial user registration and loginhow to create user registration django restframeworkcreate user registration rest framework djangodjangorest user and profilesign up user with django restuser registration and login authentication django rest frameworkhow to register new user in django rest frameworkcreate a profile with user djangouser registration django rest frameworkhow to create user and login functionality using django rest frameworkhow to get the user id who is login django rest frameworkget user id in django rest frameworkhow to create custom user model in django rest frameworkdjango rest framework custom user modelcreate user in django rest frameworkdjango rest framework custom user registration and login examplecreate user models in django rest frameworkuser registration and login system with django rest frameworkdjango rest framework user profile apidjango user profile exampledjango rest framework api get my profiledjango rest framework building custom user modeluser model django rest frameworkuser django rest frameworkhow to registyer the user with different fields in django rest franmeworkregister user django rest framework instanceof user profilecustom user django rest frameworkdjango rest framework user profilecreating a user profile using django rest frameworkuser registration in django rest frameworkdjango rest add a user in a modeldjango rest framework user registration apihow to user registration using django rest frameworkhow to create user id in django rest frameworkdjango rest user profilecustom user model django rest framework incustom user model login and registration in rest api djangohow to make custome user in django restframeworkhow to make different user in django rest frameworkdjango rest api for user registrationdjango rest framework create profile user instance django rest framework create with current user django rest framework set user as adminhow to create a user and user profile in django rest frameworkcreate custom user in django rest frameworkannonomias user in django rest frameworkdjango rest framework user modeldjango rest mange create useruser and profile djangorestcustom user model django rest frameworkuser registration and login in django rest frameworkcreate profile with user django restcreate user django apidjango rest framework user registration and login tutorialdjango rest framework user apidjango rest auth custom user modelregistration with custom user model django rest frameworkdjango rest framework user username instad of id user profile api using django and django rest frameworkregister user with rest api django how to specify the current user to the user field in django restdjango rest created by usercreate user with django rest apidjango rest get userserializer customer profilehow to create different user create in django rest frameworkdjango rest framework user registration and logindjango rest create user profile and related datadjango rest framework create user as an admindjango rest framework user registration and login exampleuser and userprofile django rest frameworkdefault api for django userdjango rest api register userdjango rest and custom user profileuser model django creation from restcreate a user using django rest frameworkdjango rest create profile with usercreate user in django restdjango rest framework user profile documentationmodelserializer fields user profile how to create user profile with django rest authuser login django rest frameworkdjango api create usercreate new user django rest frameworkupdate user profile django rest frameworkdjango rest framework authentication custom user modelcreating users with django rest frameworkdjango rest framework get userhow to create user profile with user django apicreate user django rest for profiledjango rest framework user authdjango rest framework create usercustom user model for django rest authuserprofile in user serializerhow to get specific user django rest frameworkhow to create a user and user profile in django rest framework