Assign foreing keys of both instances in the same time Django REST

I need a help, please. So I have 2 classes

class Pokemon(models.Model):
    """Pokemon object"""

    pokedex_creature = models.ForeignKey(
        PokedexCreature,
        on_delete=models.CASCADE,
    )

    trainer = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        on_delete=models.CASCADE,
        blank=True,
        null=True,
    )

    team = models.ForeignKey(
        Team,
        on_delete=models.SET_NULL,
        blank=True,
        null=True,
    )

and

class Team(models.Model):
    """Team model"""

    name = models.CharField(max_length=100)

    trainer = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        on_delete=models.SET_NULL,
        blank=True,
        null=True,
    )

    pokemon_1 = models.ForeignKey(
        Pokemon,
        on_delete=models.SET_NULL,
        blank=True,
        null=True,
    )

    pokemon_2 = models.ForeignKey(
        Pokemon,
        on_delete=models.SET_NULL,
        blank=True,
        null=True,
    )

    pokemon_3 = models.ForeignKey(
        Pokemon,
        on_delete=models.SET_NULL,
        blank=True,
        null=True,
    )

I want to put a ForeingKey of Team to Pokemon model when I add this Pokemon to the team. I use ForeingKey in Team model to assign a Pokemon to this Team so I would like to make the same this Pokemon instance to see to what Team he is assigned. What is the best way to do that?

I use Django 3.2.12 and REST Framework 3.13.1

Back to Top