В моем приложении django есть две модели: Package
и Address
Models.py
class Package(models.Model):
sender_name = models.CharField(max_length=255, default=0)
sender_company = models.CharField(max_length=255, blank=True, null=True)
sender_phone = models.CharField(max_length=255, default=0)
sender_city = models.CharField(max_length=255, default=0)
sender_state = models.CharField(max_length=255, default=0)
sender_country = models.CharField(max_length=255, default='India')
class Address(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
name = models.CharField(max_length=255, default=0)
city = models.CharField(max_length=255, default=0)
state = models.CharField(max_length=255, default=0)
country = models.CharField(max_length=255, default='India')
Я переопределяю метод post
в моем CreateAPIView
как показано ниже:
class package(APIView):
permission_classes = (permissions.IsAuthenticated,)
def post (self, request, format=None):
...
if serializer.is_valid():
serializer.save()
print("s data", serializer.data)
Address.objects.create(user=request.user.id, name=request.data["name"],
city = request.data["city"]...)
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Есть ли другой способ сохранить Address из модели пакета при создании объекта Package
??