Create a register in another table with the id as FK of the first table automatically after creating the register in the first table Django

Models.py

class Email(models.Model):
    id = models.CharField(max_length=200, primary_key=True)
    subject = models.CharField(max_length=200)
    message = models.CharField(max_length=2000)
    time = models.CharField(max_length=50)
    read = models.BooleanField()
    starred = models.BooleanField()
    hasAttachments = models.BooleanField()

class Attachments(models.Model):
    type = models.CharField(max_length=200)
    filename = models.CharField(max_length=200)
    preview = models.CharField(max_length=200)
    url = models.CharField(max_length=200)
    size = models.CharField(max_length=200)
    referring_email = models.ForeignKey(Email, related_name='email',on_delete=models.CASCADE, null=True, blank=True)

Serializers.py

class AttachmentSerializer(serializers.ModelSerializer):
    class Meta:
        model = Attachments
        fields = '__all__'#·('type','filename','preview','url','size')


class EmailSerializer(WritableNestedModelSerializer):
    #to = ToSerializer(many=True)
    #froms = FromsSerializer()
    attachments = AttachmentSerializer(many=True, required=False)

    class Meta:
        model = Email
        fields = '__all__' 
    
    def create(self, validated_data):
        #Popeo del JSON los campos Foreign Keys
        hayAttachments = False
        if(validated_data.get('attachments') != None):
            attachments_data = validated_data.pop('attachments')
            hayAttachments = True
        #Creo el objeto email a partir del JSON tocado
        email = Email.objects.create(**validated_data)  
        if(hayAttachments):
            for attachment_data in attachments_data:
                attachment = Attachments.objects.create(**attachment_data)
                attachment.referring_email = email
        return email

When I create an email, I want to automatically create an attachment and put the current email id as foreign key in the attachment table. I tried with this but only creates the attachment and set the FK=NULL, any help will be apreciated. (If you need anymore information, I'll update it)

This is the Json from wich I load the data:

{
        "id": "15459251a6d6b397565",
        "subject": "Commits that need to be pushed lorem ipsum dolor sit amet, consectetur adipiscing elit.",
        "message": "hello dear friend",
        "time": "28 Jun",
        "read": true,
        "starred": false,
        "important": true,
        "hasAttachments": true,
        "attachments": [
            {
                "type": "image",
                "filename": "flowers",
                "preview": "assets/images/etc/flowers-thumb.jpg",
                "url": "",
                "size": "1.1Mb"
            },
            {
                "type": "image",
                "filename": "snow",
                "preview": "assets/images/etc/snow-thumb.jpg",
                "url": "",
                "size": "380kb"
            },
            {
                "type": "image",
                "filename": "sunrise",
                "preview": "assets/images/etc/sunrise-thumb.jpg",
                "url": "assets/images/etc/early-sunrise.jpg",
                "size": "17Mb"
            }
        ],
        "labels": []
    }

Back to Top