Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Rest Framework - Post Foreign Key

I am new to Django Rest Framework and checked some tutorials. Now I am trying to create my own structure which is like following. I want to create a user which is OK, then create a profile seperately.

models.py

class User(models.Model):

  name = models.CharField(max_length=32)
  surname = models.CharField(max_length=32)
  facebook_id = models.TextField(null=True)
  is_sms_verified = models.BooleanField(default=False)
  created = models.DateTimeField(default=timezone.now)
  updated = models.DateTimeField(default=timezone.now)
  status = models.BooleanField(default=1)

 def __str__(self):
  return self.name+" "+self.surname

class Profile(models.Model):

  user = models.ForeignKey('User',on_delete=models.CASCADE)
  email = models.CharField(max_length=32)
  birthday = models.DateField(null=True)
  bio = models.TextField(null=True)
  points = models.IntegerField(default=0)
  created = models.DateTimeField(default=timezone.now)
  updated = models.DateTimeField(default=timezone.now)

  def __str__(self):
   return self.user.name+ " " + self.user.surname

serializers.py

class UserSerializer(serializers.ModelSerializer):

  class Meta:
    model=User
    fields = ('id','name','surname','facebook_id','is_sms_verified',)
    read_only_fields = ('created','updated')

class ProfileSerializer(serializers.ModelSerializer):
  user = UserSerializer(read_only=True)

  class Meta:
    model=Profile
    fields=('id','user','email','birthday','bio','points')
    read_only_fields = ('created','updated')

views.py

@api_view(['POST'])
def profile_create(request):
  serializer = ProfileSerializer(data=request.data)
  if serializer.is_valid():
    serializer.save()
    return JsonResponse(serializer.data, status = status.HTTP_201_CREATED)
  return JsonResponse(serializer.errors , status= status.HTTP_400_BAD_REQUEST)

data I'm trying to post

{
  "user_id": {
      "id": 2
   },
  "email": "[email protected]",
  "birthday": "1991-05-28",
  "bio": "qudur",
  "points": 31
}

The error I get;

NOT NULL constraint failed: core_profile.user_id

Where am I doing wrong? Thanks!

like image 834
Gonenc Turkekul Avatar asked May 09 '18 15:05

Gonenc Turkekul


People also ask

How do you save a foreign key field in Django REST framework?

Your code is using serializer only for validation, but it is possible use it to insert or update new objects on database calling serializer. save() . To save foreign keys using django-rest-framework you must put a related field on serializer to deal with it. Use PrimaryKeyRelatedField .

How do you get a foreign key field name instead of ID in Django REST framework?

If you want to show any field of foreign key instead of id then you can use StringRelatedField.

How do I change the foreign key in Django REST framework?

As stated in the documentation, you will need to write your own create() and update() methods in your serializer to support writable nested data. You will also need to explicitly add the status field instead of using the depth argument otherwise I believe it won't be automatically added to validated_data .

How do you pass extra context data to Serializers in Django REST framework?

In function-based views, we can pass extra context to serializer with “context” parameter with a dictionary. To access the extra context data inside the serializer we can simply access it with “self. context”. From example, to get “exclude_email_list” we just used code 'exclude_email_list = self.


2 Answers

Your ProfileSerializer has user as readonly. So you need to change that. I would suggest doing it like this

class ProfileSerializer(serializers.ModelSerializer):
    class Meta:
        model=Profile
        fields=('id','user','email','birthday','gender','bio','points')
        read_only_fields = ('created','updated')

    def to_representation(self, instance):
        self.fields['user'] =  UserSerializer(read_only=True)
        return super(ProfileSerializer, self).to_representation(instance)

If you do it this you could provide your user as plain id for POST

{
  "user": 2,
  "email": "[email protected]",
  "birthday": "1991-05-28",
  "bio": "qudur",
  "points": 31
}

And when you will read data it will look like this

{
  "user": {
    "id": 2,
    "name": "Name",
    "surname": "Surname",
    ...
  },
  "email": "[email protected]",
  "birthday": "1991-05-28",
  "bio": "qudur",
  "points": 31
}
like image 196
Sardorbek Imomaliev Avatar answered Oct 10 '22 23:10

Sardorbek Imomaliev


I've noticed Super() throws an error the way it's mentioned above in the awnser:

return super(ProfileSerializer,self).to_representation(instance)

Error: Type error, object must be an instance or subtype of type

Try the Following:

Models.py

class Program(models.Model):
    name = models.CharField(max_length=225)
    cost = models.IntegerField(default=0)
    description = models.TextField(default="", max_length=555)

class UserProgram(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    program = models.ForeignKey(Program, on_delete=models.CASCADE, related_name="program")

Serializers.py

class ProgramSerializers(serializers.ModelSerializer):
    class Meta:
        model = Program
        fields = "__all__"

class UserProgramSerializers(serializers.ModelSerializer):
    class Meta:
        model = UserProgram
        fields = "__all__"

    #IMPORTANT PART
    def to_representation(self, instance):
        response = super().to_representation(instance)
        response['program'] = ProgramSerializers(instance.program).data
        return response

Views.py

class UserProgramViewset(viewsets.ModelViewSet):
     permission_classes = [
        permissions.IsAuthenticated
     ]
     serializer_class = UserProgramSerializers
     
     def get_queryset(self):
        return UserProgram.objects.filter(user=self.request.user)    

     def perform_create(self, serializer):
        serializer.save(user=self.request.user)

When you call the GET request the following should be the output: GET Request Output

When you call the POST request you only need to pass the programID and not the whole JSON dictionary!

Hope this helped.

like image 35
PdotNJ Avatar answered Oct 11 '22 00:10

PdotNJ