Hello, I have a problem when I try to create something in database
Cannot assign "UUID('e84c8098-c240-48c2-91d9-1d266a0cd37e')": "Company.user_id" must be a "User" instance.
Here is my code
User model
class User(AbstractUser):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
email = models.EmailField(verbose_name='email address', max_length=255, unique=True)
username = models.CharField(null=True, verbose_name='username', max_length=255)
first_name = models.CharField(null=True, verbose_name='first name', max_length=255)
last_name = models.CharField(null=True, verbose_name='last name', max_length=255)
is_company_account = models.BooleanField(default=False)
session_token = models.CharField(max_length=255)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['username']
def __str__(self):
return "{}".format(self.email)
Company model
class Company(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
name = models.CharField(verbose_name='company name', max_length=50, unique=True)
company_location = models.CharField(verbose_name='company location', max_length=100)
user_id = models.ForeignKey(User, on_delete=models.CASCADE)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
def __str__(self):
return self.name
And ViewAPI
class CompanyAPIView(generics.GenericAPIView):
serializer_class = CompanySerializer
permission_classes = [IsAuthenticated]
def post(self, request):
name = request.data.get('name')
company_location = request.data.get('company_location')
user_id = request.user.id
company_serializer = CompanySerializer()
company = company_serializer.create({'name': name, 'company_location': company_location, 'user_id': user_id})
company.save()
return Response({'message': 'The company has created successfully'}, status=200)
Where can be the problem?