I am trying to create a new entry in Cassandra database using cqlengine, the first post creates a new entry, but when trying to create another entry, the last item gets updated with new values, after restarting the Django server, it works as expected (creates new entry)
My Model is
# test model udt
class address(UserType):
name = columns.Text()
age = columns.Integer()
street = columns.Text()
zipcode = columns.Integer()
class TestUserProf(Model):
user_id = columns.UUID(primary_key=True, default=uuid.uuid4())
emp_id = columns.Text()
address = columns.UserDefinedType(address)
Seializer is
# Test serialzer for test udt
class TestProfSerializer(serializers.Serializer):
user_id = serializers.CharField(max_length=100, required=False)
emp_id = serializers.CharField(max_length=50)
user_addr = serializers.DictField(required=False, source='address')
def create(self, validate_data):
"""
Create new entry in ProfSerializer
:params validate_data
:return :
"""
addr_data = validate_data.get('address')
obj = TestUserProf.objects.create(
emp_id=validate_data.get('emp_id'),
address=address(
name=addr_data.get('name'),
age=addr_data.get('age'),
street=addr_data.get('street'),
zipcode=addr_data.get('zipcode')
)
)
return TestUserProf(**validate_data)
Django View is
# Test code for udt
class TestProfView(viewsets.ModelViewSet):
""" Test class of udf
:prams request
:return list of profils
"""
model = TestUserProf
serializer_class = TestProfSerializer
def get_queryset(self):
return TestUserProf.objects.all()
def create(self, request, *args, **kwargs):
serializer = TestProfSerializer(data=request.data)
try:
if serializer.is_valid():
serializer.save()
return Response({'status': 'success'})
else:
return Response({'status': 'not valid...'})
except Exception as e:
return Response({'error': str(e)})
post data is
{
"emp_id": "EMP112",
"user_addr": {
"street": "Kochin",
"name": "Thomas John",
"zipcode": 682021,
"age": 29
}
}
it returns success, but when tried to create a new entry, the last raw gets updated,
after restarting the Django server, the new entry is getting created normally, Why does this happen?
Remove the default from your model, to generate uuid, that creates the issue, the uuid keeps on generating the same value which is the reason for updating the last row even if you create a new one.
when you create a new row, just use uuid.uuid4() at that instance