Django post_save does not get many-to-many attributes to create

I bind a method post_saveto a model in django, for example, the following code:

def save_mymodel(sender,instance,*args,**kwargs):
    print 'save called'
    for parameter in instance.parameters.all():
        print parameter.name

post_save.connect(save_mymodel, sender=MyModel)

Here are my models:

def MyModel(models.Model):
    parameters = models.ManyToManyField(Parameter)

def Parameter(models.Model):
    name = models.CharField(max_length=100)

When I try to create MyModeldjango from an administrator with any number of parameters, I get only save calledas output. If I save the same object again MyModelfrom the administrator, all parameters will be printed. What is the difference between a call saveat creation and not at creation? How can I get all the attributes of a model using post_savewhen creating it?

+4
source share
1 answer
Fields

ManyToMany save(), post_save m2m. m2m, m2m_changed.

django doc m2m_changed.

+2

Source: https://habr.com/ru/post/1627470/


All Articles