[Fixed]-Using OneToOneField with multiple versions of data

1๐Ÿ‘

โœ…

Add a boolean feild to the model and change it to true when confirmed:

class ExtraInforation(models.Model):
    company = models.OneToOneField(Company)
    wealthy = models.BooleanField()
    confirmed = models.BooleanField(default=False)

UPDATE
Based on your comment I suggest a version filed which can be a simple integer or a datetime. I would avoid creating two models at any cost ๐Ÿ™‚

class ExtraInforation(models.Model):
    company = models.ForeignKey(Company, related_name='extrainformations')
    wealthy = models.BooleanField()
    # version = models.PositiveIntegerField()
    version = models.DateTimeField(auto_now_add=True)
    confirmed = models.BooleanField(default=False)

You can add a property to Company that returns the last extrainformation so that company.extrainformation will still work:

@property
def extrainformation(self):
    return self.extrainformations.order_by("-version").first()
๐Ÿ‘คnima

Leave a comment