I am a newbie to Django Rest Framework, issue I am facing is while creating
a new record there are now new Cost Center and Role in Org valued being
created too, existing values are not being selected. If the Role in Org or
Cost Center does not exist an error should be returned, new values should
not be created.
*Serializer:*
class SectorSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Sector
fields = ('sector',)
class RegionSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Region
fields = ('region',)
class RoleSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Role
fields = ('role',)
class DepartmentSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Department
fields = ('department',)
class CompanySerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Company
fields = ('name',)
class CitySerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = City
fields = ('name',)
class CostCenterSerializer(serializers.HyperlinkedModelSerializer):
city = CitySerializer(read_only=False)
region = RegionSerializer(read_only=False)
company = CompanySerializer(read_only=False)
class Meta:
model = CostCenter
fields = ('cost_center', 'city', 'region', 'company')
class RoleInOrgSerializer(serializers.HyperlinkedModelSerializer):
department = DepartmentSerializer(read_only=False)
sector = SectorSerializer(read_only=False)
role = RoleSerializer(read_only=False)
class Meta:
model = RoleInOrg
fields = ('department', 'sector', 'role')
class HumanSerializer(serializers.HyperlinkedModelSerializer):
role_in_org = RoleInOrgSerializer(read_only=False)
cost_center = CostCenterSerializer(read_only=False)
language_primary = LanguageSerializer(read_only=False)
team_lead = serializers.RelatedField(source='team_lead', blank=True)
tns_om = serializers.RelatedField(source='tns_om', blank=True)
service_region = RegionSerializer(read_only=False)
class Meta:
model = Human
fields = ('name_first', 'name_last', 'name_common', 'email',
'channel', 'role_in_org', 'cost_center',
'language_primary', 'team_lead', 'tns_om', 'date_hire',
'date_offboard', 'date_trip_training',
'date_trip_start', 'date_nontrip_training',
'date_nontrip_start', 'date_tns_training', 'date_tns_start',
'service_region')
*Following are the corresponding models*
class CostCenter(models.Model):
cost_center = models.CharField(max_length=100, null=True)
city = models.ForeignKey(City, null=True)
region = models.ForeignKey(Region, null=False)
company = models.ForeignKey(Company, null=False)
concat_cost_center = models.CharField(max_length=100, null=True, blank=
True)
class RoleInOrg(models.Model):
department = models.ForeignKey(Department, null=False)
sector = models.ForeignKey(Sector, null=False)
role = models.ForeignKey(Role, null=False)
role_in_org = models.CharField(max_length=150, null=True, blank=True)
*API as below:*
class HumanList(APIView):
authentication_classes = (AccessKeyAuthentication,)
def get(self, request):
humans = Human.objects.all()
serializer = HumanSerializer(humans, many=True)
return Response(serializer.data)
def post(self, request):
serializer = HumanSerializer(data=request.DATA, many=True)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
else:
return Response(
serializer.errors, status=status.HTTP_400_BAD_REQUEST)
--
You received this message because you are subscribed to the Google Groups
"Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.