from django.forms import widgets
from rest_framework import serializers
from api.models import Reading
from django.contrib.auth.models import User

class ReadingSerializer(serializers.ModelSerializer):
    owner = serializers.Field(source='owner.username')
    class Meta:
        model = Reading
        fields = ('created', 'owner', 'pm10', 'pm10_reading',
                  'pm25', 'pm25_reading')

from rest_framework import serializers
from api.models import Reading
from django.contrib.auth.models import User

import datetime

class ReadingSerializer(serializers.HyperlinkedModelSerializer):
    owner = serializers.ReadOnlyField(source='owner.username')

    class Meta:
        model = Reading
        fields = ('url', 'pm10', 'pm25', 'pm10count', 'pm25count','created', 'owner','createdHour')

    def create(self, validated_data):
        return Reading.objects.create(**validated_data)

class UserSerializer(serializers.HyperlinkedModelSerializer):
    readings = serializers.HyperlinkedRelatedField(many=True, view_name='reading-detail', read_only=True)

    class Meta: 
        model = User
        fields = ('url', 'username', 'password', 'email', 'readings')
        write_only_fields = ('password',)

Add User Serializer to be able to create users from REST API
Add User Serializer to be able to create users from REST API

- from django.forms import widgets
  from rest_framework import serializers
  from api.models import Reading
  from django.contrib.auth.models import User
  
+ import datetime
+ 
- class ReadingSerializer(serializers.ModelSerializer):
+ class ReadingSerializer(serializers.HyperlinkedModelSerializer):
-     owner = serializers.Field(source='owner.username')
+     owner = serializers.ReadOnlyField(source='owner.username')
+ 
      class Meta:
          model = Reading
+         fields = ('url', 'pm10', 'pm25', 'pm10count', 'pm25count','created', 'owner','createdHour')
-         fields = ('created', 'owner', 'pm10', 'pm10_reading',
-                   'pm25', 'pm25_reading')
  
+     def create(self, validated_data):
+         return Reading.objects.create(**validated_data)
+ 
+ class UserSerializer(serializers.HyperlinkedModelSerializer):
+     readings = serializers.HyperlinkedRelatedField(many=True, view_name='reading-detail', read_only=True)
+ 
+     class Meta: 
+         model = User
+         fields = ('url', 'username', 'password', 'email', 'readings')
+         write_only_fields = ('password',)
+ 
Add User Serializer to be able to create users from REST API
## Code Before:
from django.forms import widgets
from rest_framework import serializers
from api.models import Reading
from django.contrib.auth.models import User

class ReadingSerializer(serializers.ModelSerializer):
    owner = serializers.Field(source='owner.username')
    class Meta:
        model = Reading
        fields = ('created', 'owner', 'pm10', 'pm10_reading',
                  'pm25', 'pm25_reading')

## Instruction:
Add User Serializer to be able to create users from REST API
## Code After:
from rest_framework import serializers
from api.models import Reading
from django.contrib.auth.models import User

import datetime

class ReadingSerializer(serializers.HyperlinkedModelSerializer):
    owner = serializers.ReadOnlyField(source='owner.username')

    class Meta:
        model = Reading
        fields = ('url', 'pm10', 'pm25', 'pm10count', 'pm25count','created', 'owner','createdHour')

    def create(self, validated_data):
        return Reading.objects.create(**validated_data)

class UserSerializer(serializers.HyperlinkedModelSerializer):
    readings = serializers.HyperlinkedRelatedField(many=True, view_name='reading-detail', read_only=True)

    class Meta: 
        model = User
        fields = ('url', 'username', 'password', 'email', 'readings')
        write_only_fields = ('password',)
