I'm building REST API application using Django Rest Framework. Through the post request i'm receiving data and save it with citizen_id and import_id unique_together (see the models.py). Import_id for every saving is calculating via get_current_import_id() function (see views.py) For concurrent requests i'm using "curl1 & curl2" command Probably i don't understand some basics, but what i want is to save every concurrent request (every new request should be with previous import_id + 1). Also i'm using a "current_import_id" value in serializers for create and saving instances.
P.S PATCH and GET requests are working with concurrent data. For example two concurrent PATCH requests are randomly saving (sometimes "first" is first, sometimes "first" is second, but it works)
POST request example, JSON
{
"citizen_id": 1,
"name": "Foo",
}
models.py
class CitizenInfo(models.Model):
citizen_id = models.PositiveIntegerField()
import_id = models.PositiveIntegerField()
name = models.CharField(max_length=256)
class Meta:
unique_together = ('import_id', 'citizen_id',)
views.py Getting the maximum import_id in database and determine current_import_id
def get_current_import():
previous_import_id = CitizenInfo.objects.aggregate(
Max('import_id'))['import_id__max']
if not previous_import_id:
previous_import_id = 0
current_import_id = previous_import_id + 1
return current_import_id
At the moment the result statuses i getting from POST "curl1 & curl2" command are 201 and 500 (sometimes 500 and 201 due to randomness).
By the way, i've tried django F()
and select_for_update(nowait=True)
but it doesn't help as it expected
Project configuration: Django + Gunicorn + Nginx
Any advise, please.