Sending data to two different fields using foreign key
I am using foreign key of a model person using both its field in other other model company. But while inserting data in company I am not getting last name, output is employee and last are both the same.
Class Person (models.Model):
first_name = models.CharField(max_length=30,null=True)
last_name = models.CharField(max_length=30,null=True)
def str(self):
return str(self.first_name)
Class Company (models.Model):
name=models.CharField(max_length=20,null=True)
employee=models.ForeignKey(Person,on_delete=models.CASCADE,related_name='first',null=True)
last=models.ForeignKey(Person,on_delete=models.CASCADE,related_name='second',null=True)
def str(self):
return str(self.name)
ViewSet
class CompanyViewset(viewsets.ViewSet):
def create(self,request):
name=request.data.get('name')
employee=request.data.get('employee')
last=request.data.get('last')
user=Company()
user.name=name
user.employee=Person.objects.get(first_name=employee)
user.last=Person.objects.get(last_name=last)
#print(user.last)
#user.last=use.last_name
print(user.last.last_name)
user.save()
return Response({'Successfully saved'})
Top comments (0)