I'm new in Django realm but see there is a lot of "magic" there. I'm using Django REST Framework and creating app that will allow free user registration. My user needs some additional fields that are not available in Django user. So I googled for extending User. There is an idea that this should be done by creating something like this
class MyUser(models.Model):
user = models.ForeignKey(User, unique=True)
city = models.CharField(max_length=50, blank=True, default='')
This is fine but I have this serializer
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = MyUser
fields = ('id', 'username', 'password', 'first_name', 'last_name', 'email', 'city')
So, the problem is that this serializer does some "magic" here. It tries to figure out what field should model have ... I want to have user with fields listed here, and these are fields are in User and 'city' is new custom field. Serializer doesn't get that it should look inside User model.
What am I missing here? How to tell this serializer that I want some fields inside User? I need to be able to crete user too.
OneToOneFieldfor your UserModel relation, notForeignKeysince their can be only oneMyUserfor everyUserinstance.OneToOneFieldand follow this answer and you're good to go: link