I want to send Response(xyzSerializer.data), where xyzSerializer serializes data form two models i.e. Users and Customers to give it following body/look
{
'userID': 1,
'firstName': 'name',
'lastName': 'Lname',
'gender': 'Male',
'country': 'Cloc',
'city': 'City',
'phoneNumber': '12345',
'dob': 'dd-mm-yy',
'dateJoined': 'dd-mm-yy',
'role': 'blogger',
'user': 1
'customerID': 1,
'occupation': '',
'blogger': True,
'userID': 1
}
But, currently I can either serialize Users, or Customers model individually which give following output respectively,
UserSerializer
{
'userID': 1,
'firstName': 'name',
'lastName': 'Lname',
'gender': 'Male',
'country': 'Cloc',
'city': 'City',
'phoneNumber': '12345',
'dob': 'dd-mm-yy',
'dateJoined': 'dd-mm-yy',
'role': 'blogger',
'user': 1
}
CustomerSerializer
{
'customerID': 1,
'occupation': '',
'blogger': True,
'userID': 1
}
I want to join data from two models using
userID
and send that as a response.
Here are my Users and Customers models,
class Users(models.Model):
userID = models.AutoField(primary_key=True)
user = models.OneToOneField(User, on_delete=models.CASCADE)
firstName = models.TextField()
lastName = models.TextField()
gender = models.TextField()
country = models.TextField()
city = models.TextField()
phoneNumber = models.TextField()
dob = models.TextField()
dateJoined = models.TextField()
role = models.TextField()
def __str__(self):
return f"ID: {self.userID}"
class Customers(models.Model):
customerID = models.AutoField(primary_key=True)
userID = models.ForeignKey('Users', on_delete=models.CASCADE, name="userID")
occupation = models.TextField()
blogger = models.BooleanField(default= False)
def __str__(self):
return f"ID: {self.customerID}"
Here are my Serializers,
class UserSerializer(ModelSerializer):
class Meta:
model = Users
fields = '__all__'
class CustomerSerializer(ModelSerializer):
class Meta:
model = Customers
fields = '__all__'
Here is my view:
@api_view(['GET'])
def getUserByIDDemo(request, uName, pWord):
username = uName
password = pWord
userObj = authenticate(request, username=username, password=password)
# Check if authentication successful
if userObj is not None:
login(request, userObj)
currentUser = Users.objects.get(user = userObj)
userializer = UserSerializer(currentUser, many = False)
customer = Customers.objects.get(customerID = userializer.data['userID'])
cserializer = CustomerSerializer(customer, many = False)
print(userializer.data)
print(cserializer.data)
#following two lines I tried concatinating data but that gave error
#xyz = cserializer.data userializer.data
#print(xyz)
#Here I want to send data from both models joined by userID
return Response(cserializer.data)
else:
message = "Invalid username and/or password."
return Response(message)
Note: If my question is unclear please reach out to me for clarification
CodePudding user response:
Use nested serializer:
class CustomerSerializer(ModelSerializer):
class Meta:
model = Customers
fields = '__all__'
class UserSerializer(ModelSerializer):
customers_set = CustomerSerializer(many=True)
class Meta:
model = Users
fields = '__all__'
Then just use UserSerializer
in your view:
...
userializer = UserSerializer(currentUser)