THE PROBLEM
Whenever I send data through post, the data gets validated, I get an output with save() method(I get a PK implying that the object was saved?) but the creation does not take place in the DB.
I tried running UsrInDys.objects.create() from the shell and everything seemed to work fine from the shell. Just not from the django rest serializer method.
I turned on my PyCharm Debugger and went deep into the .save() serializer method. It essentially did the same thing, Model.objects.create(**validated_data) and got an object(which implies the change to DB did take place, right?). I get the same kind of output when I try to create an object from the shell. But the object creation also takes place in the DB when I do it from the shell.
System Details:
> PostgreSQL version 11.0
> Python 3.5
> Django 2.2.1
> Django-restframework 3.8.2
> Linux Mint 18.3
#INSIDE APIVIEW CLASS
def post(self, request):
usr_in_dys_serializer = UserInDysSerializer(data=request.data)
if usr_in_dys_serializer.is_valid():
saved_usr_in_dys = usr_in_dys_serializer.save(no_of_days=no_of_days) # Output: UsrInDys object (2068) <---- but not being saved to the Database....
#INSIDE SERIALIZER.PY
class UserInDysSerializer(serializers.ModelSerializer):
class Meta:
model = UsrInDys
strt_day = serializers.DateField()
end_day = serializers.DateField()
fields = ('strt_day', 'end_day', 'no_of_days')
extra_kwargs = {
'no_of_days': {'read_only': True}
}
#INSIDE MODEL.PY
class UsrInDys(models.Model):
trip_id = models.AutoField(primary_key=True)
strt_day = models.DateField()
end_day = models.DateField()
no_of_days = models.IntegerField()
class Meta:
managed = False
db_table = 'usr_in_dys'
def __unicode__(self):
return [self.strt_day, self.end_day]
#INSIDE SETTTINGS.PY
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'postgres',
'USER': env("USER"),
'PASSWORD': env("PASSWORD"),
'HOST': env("HOST"),
'PORT': '5433',
}
}
DATABASES['default']['ATOMIC_REQUESTS'] = True