Comments (7)
@mateusz-sikora I've seen your solution while looking for somthing similar for myself, and tried to come up with somthing a bit more general which doesn't make you add each field seperatly throughout the code. Came up with this -
class UserProfile(models.Model):
user = models.OneToOneField(User, unique=True)
phone_number = models.CharField(blank=True,null=True,max_length=20)
access_token = models.CharField(blank=True,null=True,max_length=140)
email_sub = models.BooleanField(default=False)
class UserSerializer(UserDetailsSerializer):
phone_number = serializers.CharField(source="userprofile.phone_number")
access_token = serializers.CharField(allow_blank=True,source="userprofile.access_token")
email_sub = serializers.BooleanField(source="userprofile.email_sub")
class Meta(UserDetailsSerializer.Meta):
fields = ['pk', 'username', 'email', 'first_name', 'last_name'] + \
[f.name for f in UserProfile._meta.get_fields()]
del fields[fields.index('user')]
def update(self, instance, validated_data):
raise_errors_on_nested_writes('update', self, validated_data)
info = model_meta.get_field_info(instance)
profile = instance.userprofile
p_info = model_meta.get_field_info(profile)
for attr, value in validated_data.items():
if attr == 'userprofile':
for attr2, value2 in value.items():
setattr(profile, attr2, value2)
elif attr in info.relations and info.relations[attr].to_many:
field = getattr(instance, attr)
field.set(value)
else:
setattr(instance, attr, value)
instance.save()
profile.save()
return instance
from django-rest-auth.
class ProfileSerializer(serializers.ModelSerializer):
avatar = serializers.CharField(source='profile.avatar')
class Meta:
model = User
fields = ['id', 'username', 'email', 'avatar']
def save(self, **kwargs):
profile = self.validated_data.pop('profile')
instance = super().save(**kwargs)
Profile.objects.update_or_create(user=instance, defaults=profile)
return instance
from django-rest-auth.
@jmorrice @Omnipresent I added new position in FAQ, should be helpful for both of you http://django-rest-auth.readthedocs.org/en/latest/faq.html (point #2)
from django-rest-auth.
+1 for supporting extended user profiles!
from django-rest-auth.
@Omnipresent
I recommend creating fields from UserProfile model directly in UserSerializer, like this:
class UserSerializer(serializers.ModelSerializer):
company_name = models.CharField(max_length=100)
and then update UserProfile
object inside update
method.
Your solution will not work because you can update only UserProfile id assigned to User, not fields from UserProfile.
from django-rest-auth.
@jmorrice You can write your custom serializer to do that.
from django-rest-auth.
@Idanraman do you have this code within a github codebase so that i can understand it fully. For example what is UserDetailsSerializer?
from django-rest-auth.
Related Issues (20)
- Password Reset Confirm Expect parameter which is not in email HOT 1
- TEMPLATE DOES NOT EXIST
- Option to just need one password field for new account / reset/change password. HOT 2
- Can't login without email even though it's supposed to be optional
- Django and react login with google HOT 2
- TypeError: 'PhoneNumber' object is not subscriptable
- Deprectated import ugettext_lazy from django.utils.translation HOT 1
- ConnectionRefusedError: Registration endpoint response
- Update for Django 4.0 HOT 6
- Reverse for 'password_reset_confirm' not found. 'password_reset_confirm' is not a valid view function or pattern name. HOT 6
- Wrong response on using login without verifying the user HOT 1
- `LogoutView`'s non-compliance with `drf-spectacular`
- ImportError: cannot import name 'url' from 'django.conf.urls' HOT 6
- The package doesn't work with django 4.0.*
- The package doesn't work with django 4.0.* HOT 1
- The package doesn't work with django 4.0
- The package doesn't work with django 4.0.*
- ImproperlyConfigured at /api/v1/auth/registration/account-email-verification-sent/
- The package doesn't work with Django from 3.12
- Failed to run with the lates allauth lib HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from django-rest-auth.