I have following model in my Django Model
class Match(models.Model):
initiator = models.ForeignKey('CustomUser', related_name='match_initiator', on_delete=models.CASCADE)
matched_person = models.ForeignKey('CustomUser', related_name='matched_person', on_delete=models.CASCADE)
created_at = models.DateTimeField(auto_now=True)
algo_version = models.IntegerField()
room_no = models.ForeignKey('Room', related_name='room_match', on_delete=models.CASCADE, null=True, blank=True)
class Room(models.Model):
name = models.CharField(max_length=20, unique=True)
members = models.ManyToManyField(CustomUser, related_name="joined_rooms")
created_at = models.DateTimeField(auto_now=True,editable=False)
is_active = models.BooleanField(default=True)
So basically I have two models, Room and Match with Match having a foreign key to a room. Now I want to create both of these objects at once, but I can not do it as first I have to create Room object get the id of it and assign it to Match object instance and then save it again. Problem with this is that if any of the model save is failed, it breaks the code.
Is there any way I can create both Match and Room object at once?
I have found this reference in Django Documentation: But I am not sure whether Django handle it automatically or do I need to handle it.
Thanks in advance!!!