0

Assume I have a Django application where a user may create a fantasy football team Team. This is accomplished using TeamCreateView, a regular CreateView (class-based generic). The User model has a field called my_team, which by default is empty. What is the best way to ensure that after User creates a new Team, my_team will be set to the newly created model instance?

Edit: models, as requested:

class Team(models.Model):
    name = models.CharField(max_length=127)
    # ...some other stuff

class User(models.Model):
    email = models.EmailField(max_length=127)
    first_name = models.CharField(max_length=127)
    last_name = models.CharField(max_length=127)
    my_team = models.ForeignKey(Team, null=True, blank=True)

The relationship is 1-to-1, but cannot be done as a OneToOne field as they are going to be created at different times. That is, the way the business process is structured calls for a user to first create an account (creating the User entity), and then either get invited into an existing team or create a team. The Team is created using a generic CreateView, and my main objective is to attach the Team to the User using the my_team field.

Chris vCB
  • 1,023
  • 3
  • 13
  • 28

0 Answers0