I use Django 2.0 and mysql database and my project model is like:
class Parent(models.Model):
name = models.CharField()
class Child(models.Model):
number = models.IntegerField()
parent = models.ForeignKey(Parent)
what I want is to save both parent and child object together at the same time so that if in any case the child object has error(like number field is "some text") and can not be saved the parent object doesn't save.
in Flask (with postgresql) there is add(object) and add_all([parent, child]) methods and I used add_all so if child has error parent doesn't save neither.
but in Django I couldn't find this method.
the default method is:
parent = Parent(name = "my name")
parent.save()
child = Child(number=3, parent=parent)
child.save()
what I want is something like this:
parent = Parent(name = "my name")
child = Child(number=3, parent=parent)
and then:
child.save(with related parent)
or:
save(parent, child together)
PS: I read this link and I think "SET" method is what I need but I'm not sure how to use it and if it's the solution: django relations