I am running a Django app on Heroku that uses Django REST Framework to store data from scanners, which are devices which send a JSON POST request to the framework whenever it detects a new scan. The request is serialized and creates a new scan object. The models are as follows:
class Location(models.Model):
address = models.CharField(max_length=300)
city = models.CharField(max_length=50)
state = models.CharField(max_length=100)
zipcode = models.IntegerField(null=True, blank=True)
latitude = models.FloatField(null=True, blank=True)
longitude = models.FloatField()
_current_occupancy = models.IntegerField(default=0, db_column='current_occupancy')
def __str__(self):
return self.address
@property
def current_occupancy(self):
scanner_objs = Scanner.objects.filter(location=self)
num_scans = 0
for s in scanner_objs:
today_min = datetime.datetime.combine(datetime.date.today(), datetime.time.min)
today_max = datetime.datetime.combine(datetime.date.today(), datetime.time.max)
scan_objs = Scan.objects.filter(scanner=s, datetime__range=(today_min,today_max))
num_scans += len(scan_objs)
return num_scans
class Scanner(models.Model):
location = models.ForeignKey(Location, on_delete=models.CASCADE, null=True, blank=True,related_name='scanner')
description = models.CharField(max_length=300, null=True, blank=True)
def __str__(self):
return self.location.address + ": " + self.description + " (id=" + str(self.pk) + ")"
class Scan(models.Model):
scanner = models.ForeignKey(Scanner, on_delete=models.CASCADE, null=True, blank=True,related_name='scan')
datetime = models.DateTimeField(null=True, blank=True, default=datetime.datetime.now)
def __str__(self):
if self.scanner != None:
return self.scanner.description + ": " + str(self.datetime)
else:
return "None: " + str(self.datetime)
class Meta:
ordering = ('datetime',)
I'm writing code locally and pushing to Heroku with Git. When I create any object locally and push it, the object saves correctly, but when I try to create a new object on Heroku by serializing a POST request it seems to save the object for a few minutes and then abruptly drops it. I check the list of objects on Django REST Framework's browsable API and it seems to forget I created the new object after a few minutes, as if it is not saving in the database correctly. What might be the problem here? Is Heroku not saving new objects in the PostgreSQL database because my local DB file is writing over it?