2

I'm using Django 1.8.2 with python 2.7.3, rest framework and allauth. I'm trying to extend django.contrib.auth.models.User for adding custom fields, but the returned json is empty and there are Users created:

In models.py

from django.contrib.auth.models import User
from django.db import models

class MyUser(models.Model):
    user = models.OneToOneField(User)
    black_coffee = models.IntegerField(default=0)
    coffee_with_milk = models.IntegerField(default=0)
    coffee_cut = models.IntegerField(default=0)

In serializers.py

from rest_framework import serializers
from cafeterias.models import MyUser

class UserSerializer(serializers.HyperlinkedModelSerializer):
    # snippets = serializers.HyperlinkedRelatedField(many=True, view_name='snippet-detail', read_only=True)

    class Meta:
        model = MyUser
        fields = ('username', 'black_coffee', 'coffee_with_milk', 'coffee_cut')

In views.py

from cafeterias.models import MyUser
from rest_framework import permissions
from rest_framework import viewsets

class UserViewSet(viewsets.ReadOnlyModelViewSet):
    queryset = MyUser.objects.all()
    serializer_class = UserSerializer
    permission_classes = (permissions.AllowAny,
                          IsOwnerOrReadOnly,)

Here is the result of the json:

HTTP 200 OK Content-Type: application/json Vary: Accept Allow: GET, HEAD, OPTIONS

{ "count": 0, "next": null, "previous": null, "results": [] }

3
  • why are you doing it via a OneToOne field? docs.djangoproject.com/en/1.8/topics/auth/customizing/… ...you can't directly access a field on the base model (eg username) if you use the onetoone method, and it sounds like you're currently creating django auth User without creating associated MyUser Commented Jul 21, 2015 at 17:08
  • Because I'm trying to get the fields of the User. Should I inherit from AbstractBaseUser. AbstractBaseUser ? Commented Jul 21, 2015 at 17:19
  • 1
    yes, extend AbstractUser instead, it'll make everything easier. Remember to set the AUTH_USER_MODEL setting docs.djangoproject.com/en/1.8/topics/auth/customizing/… Commented Jul 21, 2015 at 20:22

2 Answers 2

5

You are not extending the User model correctly.

To extend the User model with your extra fields, we can use AbstractUser model. This will provide me with all the User fields like username, email and etc. Now, we can add all the extra fields in MyUser model.

We need to do something like:

models.py

from django.contrib.auth.models import AbstractUser

class MyUser(AbstractUser):
    black_coffee = models.IntegerField(default=0)
    coffee_with_milk = models.IntegerField(default=0)
    coffee_cut = models.IntegerField(default=0)

This will provide all the User fields along with the 3 extra fields in MyUser model.

Sign up to request clarification or add additional context in comments.

3 Comments

I'm still getting an empty json: { "count": 0, "next": null, "previous": null, "results": [] }
Do any objects exist in the db since now you have changed the model. It seems like there are no objects in the db.
It finally works, I added AUTH_USER_MODEL ='myapp.Myuser' on settings and deleted and recreated the database, Thank you
0

This should really just be a comment, but I don't have enough reputation to post a comment.

It seems to me that what you need is a user profile model. Here you can see how it's done. Notice the post_save.connect(...) call. It's required in order to automatically create a new profile each a new user is created, and it seems like that's exactly what you're missing.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.