I'm building a django-based application that receives some information from client-computers (e.g. memory) and saves it into the database.

For that I created the following model:

class Machine(models.Model):
   uuid = models.UUIDField('UUID', primary_key=True, default=uuid.uuid4, editable=False)
   name = models.CharField('Name', max_length=256)
   memory = models.OneToOneField('Memory', on_delete=models.CASCADE, null=True)

   def save_data(self, data):
        if not self.memory:
            memory = Memory()
            memory.save()
            self.memory = memory
            self.save()
        self.memory.total = data['memory_total']
        self.memory.used = data['memory_used']
        self.memory.cached = data['memory_total']
        self.memory.save()

Now for the Memory, I have the following model:

class Memory(models.Model):
    total = models.IntegerField(default=0)
    used = models.IntegerField(default=0)
    cached = models.IntegerField(default=0)

To save data to the machine-model when I receive it from the client, I call the save_data()-method. There I test if there is already a self.memory object, and if not I create it first before adding the data.

Now, even tho it's working as intended I was wondering if there was a better, more clean way to achieve this. Would it be possible to initialize all my OneToOne-Fields to an empty instance of the referenced type so I needn't do the if not every time?


Fields accept a default keyword argument. This can be a callable that returns a value. You can make a callable that returns the appropriate value; in this case, the primary key of a newly created Memory object.

def default_memory():
    mem = Memory()
    mem.save()
    return mem.pk

class Machine(models.Model):
    ...
    memory = models.OneToOneField('Memory', on_delete=models.CASCADE, null=True, default=default_memory)