user3564183 user3564183 - 7 months ago 61
Python Question

Django Python 'Customer' object is not iterable

from django.db import models
class Customer(models.Model):

cust_firstname=models.TextField(max_length=50)
cust_lastname=models.TextField(max_length=50)
cust_company=models.TextField(max_length=100)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
cust_contact_number = models.IntegerField()
cust_email = models.TextField(max_length=100)
cust_address=models.TextField(max_length=200,default=None)

class Employee(models.Model):

employee_firstname = models.TextField(max_length=50)
employee_lastname = models.TextField(max_length=50)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
employee_contact_number = models.IntegerField()
employee_email = models.TextField(max_length=100)
employee_designation = models.TextField(max_length=50)
employee_address=models.TextField(max_length=200, default=None)

class ProjectDetails(models.Model):

customer = models.ForeignKey(Customer)
employee=models.ForeignKey(Employee)
project_name = models.TextField(max_length=50)
project_startdate = models.DateTimeField(auto_now=False, default=None)
project_status = models.TextField(default="Initial")
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)


The above code is my model declaration
and my Serializer class is

from ProjectTemplate.models import Customer, Employee, ProjectDetails
from rest_framework import serializers

class CustomerSerializers(serializers.ModelSerializer):
class Meta:
model=Customer
fields = ('id','cust_firstname','cust_lastname','cust_company','created_at','updated_at','cust_contact','cust_email','cust_address')

read_only_fields = ('created_at', 'updated_at')

class EmployeeSerializer(serializers.ModelSerializer):
class Meta:
model=Employee
fields = ('id','employee_firstname','employee_lastname','created_at','updated_at','employee_contact','employee_email','employee_designation','employee_address')

read_only_fields = ('created_at', 'updated_at')

class ProjectDetailsSerializer(serializers.ModelSerializer):
customer = CustomerSerializers(many=True, read_only=True)
employee = EmployeeSerializer(many=True, read_only=True)
class Meta:
model = ProjectDetails
fields = ('id','project_name','project_startdate','created_at','updated_at','project_status','customer','employee')
read_only_fields = ('created_at', 'updated_at')


In my view i have the below code

def get(self, request, format=None):
queryset = ProjectDetails.objects.all()
projectid = self.request.query_params.get('pk', None)
if projectid is not None:
queryset = queryset.get(id=projectid)
serializer = ProjectDetailsSerializer(queryset, many=False)
return Response(serializer.data)
else:
serializer = ProjectDetailsSerializer(queryset, many=True)
return Response(serializer.data)


And my URL for the above view is

url(r'^api/v1/projects/$', ProjectListView.as_view()),


And when i try to access the URL on my Browser i get TypeError. Im trying to get all the Customers and Employees who belong to a project. But it fails can any one help me to fix this.

enter image description here

AKS AKS
Answer

I'm trying to get all the Customers and Employees who belong to a project.

I am not sure what do you want to achieve here because looking at your model, an instance of ProjectDetail will only have one customer and one employee:

class ProjectDetails(models.Model):
    customer = models.ForeignKey(Customer)
    employee=models.ForeignKey(Employee)

Considering this, using many=True doesn't make any sense in the serializer. So this is what causing the error:

class ProjectDetailsSerializer(serializers.ModelSerializer):
    customer = CustomerSerializers(many=True, read_only=True)
    employee = EmployeeSerializer(many=True, read_only=True)

UPDATE: To show a specific field from the related object:

Based on the comments in the answer the OP want to show the name of customer or employee instead of id.

It can be achieved using a SlugRelatedField:

class ProjectDetailsSerializer(serializers.ModelSerializer):
    customer = serializers.SlugRelatedField(slug_field='cust_firstname', read_only=True)
    employee = serializers.SlugRelatedField(slug_field='employee_firstname ', read_only=True)
    # other fields

Do note that using SlugRelatedField you can get only one field as in the example above you would get firstname for both.

Comments