I have created an API in DRF which accepts POST request with some data, But sometimes I feel that same requests are coming in parallel which causing the duplicate data in DB.
class Feedback(models.Model):
user = models.ForeignKey(Student)
message = models.CharField(max_length=255)
Use can send same feedback multiple time. Let's think it's an open API so anyone can consume it and in someone's app user clicks multiple time on button and I received multiple requests but data should be saved only one time.
I have tried by adding a BooleanField to Student table and used the following code to prevent it. But as multiple requests coming in parallel they can read the same value True.
if student.can_submit_feedback:
student.can_submit_feedback = False
student.save()
else:
# Code for saving feedback
student.can_submit_feedback = True
student.save()
I want to process only one API call on same endpoint and same IP at a time. How can I achieve it?
Updated
I have researched and found that we can add lock on table or object but I am looking for prevention on requests level