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": [] }

david
  • 415
  • 1
  • 9
  • 21
  • why are you doing it via a OneToOne field? https://docs.djangoproject.com/en/1.8/topics/auth/customizing/#specifying-a-custom-user-model ...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` – Anentropic Jul 21 '15 at 17:08
  • Because I'm trying to get the fields of the User. Should I inherit from AbstractBaseUser. AbstractBaseUser ? – david Jul 21 '15 at 17:19
  • 1
    yes, extend `AbstractUser` instead, it'll make everything easier. Remember to set the `AUTH_USER_MODEL` setting https://docs.djangoproject.com/en/1.8/topics/auth/customizing/#substituting-a-custom-user-model – Anentropic Jul 21 '15 at 20:22

2 Answers2

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.

Rahul Gupta
  • 46,769
  • 10
  • 112
  • 126
  • I'm still getting an empty json: { "count": 0, "next": null, "previous": null, "results": [] } – david Jul 21 '15 at 17:49
  • Do any objects exist in the db since now you have changed the model. It seems like there are no objects in the db. – Rahul Gupta Jul 21 '15 at 18:08
  • 2
    It finally works, I added AUTH_USER_MODEL ='myapp.Myuser' on settings and deleted and recreated the database, Thank you – david Jul 23 '15 at 23:06
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.

Community
  • 1
  • 1
JacekMiszczak
  • 31
  • 1
  • 5