Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django save previous object from models

In a Django admin site, I have this class. I want to save a previous version of a object (Servers) which is a manytomany field to find changes on the object.

With normal CharField this work, but for manytomany fields I got this error:

"<SourceDestinationGroup: asdas>" needs to have a value for field "id" before this many-to-many relationship can be used.

here is my objectclass

class SourceDestinationGroup(models.Model):
    STATE_CHOICES = (
        ('C', 'in Change'),
        ('F', 'Finished')
        )
    ServerGroupName = models.CharField(max_length=256)
    Description = models.CharField(max_length=256,blank=True)
    Servers = models.ManyToManyField(Server)
    Status = models.CharField(max_length=1, choices=STATE_CHOICES, default='C')



    def __init__(self, *args, **kw):
        super(SourceDestinationGroup, self).__init__(*args, **kw)
        self._old_Servers = self.Servers

    def save(self, **kw):
       if self.Servers != self._old_Servers:
            self.Status = 'C'
            self._old_Servers = self.Servers

        super(SourceDestinationGroup, self).save(**kw)



   def __str__(self):
       return self.ServerGroupName
like image 579
Christian Frei Avatar asked Mar 13 '18 11:03

Christian Frei


2 Answers

M2M relationships aren't saved as part of the save() method. In the admin, the main object is saved, and then the m2m relation is saved; so, by serializing the list of tags in the save method, you're printing the value of the tags before the new values have been saved. If you want to install "post m2m save" behavior, you'd need to override the update view on the admin itself.

like image 113
Christian Frei Avatar answered Sep 22 '22 19:09

Christian Frei


You are trying to initialize the ManyToMany attribute (.Servers) when creating the object..

Wherever you try to create the SourceDestinationGroup, you should probably create it THEN do:

group.Servers = servers
group.save()

Or, remove the Servers attribute in the init method and reset it afterwards:

def __init__(self, *args, **kw):
    super(SourceDestinationGroup, self).__init__(*args, **kw)
    self._temp_Servers = self.Servers

def save(self, **kw):
    if self.id is None:
        self._temp_Servers = self.Servers
        self.Servers = None # Your 'Servers' attribute was still being set, hence raising an Exception in the super.save just after
    super(SourceDestinationGroup, self).save(**kw)
    if self._old_Servers is not None:
        self.Servers = self._temp_Servers 
        super(SourceDestinationGroup, self).save(**kw)
like image 39
ameunier Avatar answered Sep 21 '22 19:09

ameunier