0

I have three models with historical records:

class WifiUser(models.Model):
    ....
    wifiDevice = models.OneToOneField(WifiDevice, on_delete=models.CASCADE, default=None)
    wifiSim = models.OneToOneField(WifiSim, on_delete=models.CASCADE, default=None)
    history = HistoricalRecords()

class WifiDevice(models.Model):
    ....
    history = HistoricalRecords()

class WifiSim(models.Model):
    ....
    history = HistoricalRecords()

I want to keep track of history with corresponding foreign key history records. But when accessing the history of Wifiuser I get the latest values of WifiDevice and WifiSim. I want the historical record of WifiDevice and WifiSim to point to that record of their one. Whats the best method to follow for this ?

Rashed Mazumder
  • 121
  • 1
  • 9

3 Answers3

0

To keep track of the history and not have only the last object show,I suggest you add a model for the history,which contains the wifiUser wifiDevice and wifiSim,and maybe add a date for the access date and time, for auto adding the date I suggest you look at this answer (Django auto now and auto add now)

B Z Ilyes
  • 28
  • 4
  • Hello, I think your approach may be the way to go. But how do you suggesting adding a model for the history with foreign keys? As the foreign key will point to the latest object. – Rashed Mazumder Jun 20 '20 at 04:50
  • You will be adding to the database,so each foreign key will be saved to their according object and having new foreign keys won't cause the data to be lost,just make sure you don't have a unique together attribute because that will cause integrity errors when having maybe a user that uses multiple devices. – B Z Ilyes Jun 20 '20 at 09:32
0

I saw your post in Upwork , you need to override the str method of both models WifiDevice & WifiSim as the following :


class WifiDevice(models.Model):
    ....
    history = HistoricalRecords()

    def __str__ (self):
    return self.history // the solution 




class WifiSim(models.Model):
    ....
    history = HistoricalRecords()
    def __str__ (self):
    return self.history // the solution 

itjuba
  • 518
  • 4
  • 23
-2
class BilingualCorpus(models.Model):
    file_url = models.FileField(upload_to='corpus_files/', validators=[FileExtensionValidator(allowed_extensions=['txt', 'csv', 'tmx', 'xlsx'])])
    file_name = models.CharField(max_length=255, default='none')
    name = models.CharField(max_length=50,default='none')
    s_lang = models.CharField(max_length=5,default='en')
    t_lang = models.CharField(max_length=5,default='th')
    note = models.TextField(blank=True)
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    created_at = models.DateTimeField(auto_now_add=True, blank=True)
    updated_at = models.DateTimeField(auto_now=True, blank=True)

class CorpusStatus(models.Model):
    status = models.CharField(max_length=50, default='Unchecked')

class  BilingualSentence(models.Model):
    corpus = models.ForeignKey(BilingualCorpus, on_delete=models.CASCADE)
    source = models.TextField(blank=True)
    target = models.TextField(blank=True)
    status = models.ForeignKey(CorpusStatus, blank=True, null=True, on_delete=models.SET_NULL)
maksim
  • 1
  • 3
    Can you provide an explanation of how this solves the problem? Please be sure to use the edit button to add the explanation directly to your answer, instead of leaving it here in the comments. – Jeremy Caney Jun 24 '20 at 15:59