GithubHelp home page GithubHelp logo

Comments (7)

Idanraman avatar Idanraman commented on July 18, 2024 3

@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.

POD666 avatar POD666 commented on July 18, 2024 2
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.

mateusz-sikora avatar mateusz-sikora commented on July 18, 2024 1

@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.

jmorrice avatar jmorrice commented on July 18, 2024

+1 for supporting extended user profiles!

from django-rest-auth.

mateusz-sikora avatar mateusz-sikora commented on July 18, 2024

@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.

mateusz-sikora avatar mateusz-sikora commented on July 18, 2024

@jmorrice You can write your custom serializer to do that.

from django-rest-auth.

rnjailamba avatar rnjailamba commented on July 18, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.