Django REST Framework - the best way to use POST request to other model API endpoint?
I have two models - with users profiles and with events. When the user profile is creating, I want to create for him the timetable with events. When I get the events[] list - I want to send all events using POST to Event model API's in url 127.0.0.1:8000/api/all-events/. What is the simplest way to do that?
Event models.py:
class Event(models.Model):
user_name = models.CharField(max_length=128, default='')
event = models.CharField(max_length=128, default='')
date = models.DateField(default='')
Profiles view.py:
class ProfilesList(generics.ListAPIView):
queryset = Account.objects.all()
serializer_class = AccountSerializer
filter_backends = [DjangoFilterBackend]
filterset_fields = ['user_name']
def post(self, request, format=None):
saveserialize = AccountSerializer(data=request.data)
if saveserialize.is_valid():
saveserialize.save()
timetable = Timetable(request.data["user_name"], 2021)
events = timetable.create_timetable()
for event in events:
// Here I want to put the .post request to every event. I want to send
// {'username': event[0], 'event': event[1], 'date': event[2]} to
// Event model API's on url /api/all-events/
return Response("it's working")