Note that there are some explanatory texts on larger screens.

plurals
  1. PORegistration of an extended user through POST request
    primarykey
    data
    text
    <p>I am using Django v1.7dev with the Django Rest Framework v2.3 </p> <p>I have extended the user Class as follows:</p> <pre><code>class UserProfile(models.Model): user = models.OneToOneField(User) gang = models.ForeignKey('Gang', related_name='gangsters') def __unicode__(self): return self.user.username User.profile = property(lambda u: UserProfile.objects.get_or_create(user=u)[0]) </code></pre> <p>I have also created a User serializer endpoint (which does not show the password when I sent a GET request, but accepts it when I send a post, with the purpose of using it for registration:</p> <pre><code>class UserSerializer(serializers.ModelSerializer): class Meta: model = User fields = ('id', 'username', 'password', 'email') def restore_object(self, attrs, instance=None): user = super(UserSerializer, self).restore_object(attrs, instance) user.set_password(attrs['password']) return user def to_native(self, obj): ret = super(UserSerializer, self).to_native(obj) del ret['password'] return ret </code></pre> <p>I'd love to extend the serializer to include also the gang parameter. Something similar to</p> <pre><code>gang = serializers.Field(source='profile.gang') </code></pre> <p>but writable instead of ReadOnly, so that when I register a user I can insert also the gang, possibly in an atomic way. What is the best approach in this case? I have tried to play a bit with other fields types, but unsuccessfully till now. At the moment I am sending two separate post (one for User and one for UserProfile, but I bet there is a better way...)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload