[Answer]-Assign an Instance in a ForeingKey in Django

1👍

match and bet_user_a are foreign keys so you can’t assign simple strings/integers to these fields. So you have to get the instances of FK models from the DB and assign them in the create() call:

ClosedBets.objects.create(
                   match=Match2x1.objects.get(pk=request.POST['match']),
                   user_a=self.request.user,
                   bet_user_a=Teams.objects.get(pk=request.POST['team']),
                   user_b=bet.user_id,
                   bet_user_b=bet.match_id)

Also note that the field in the ClosedBets model is called match but you trying to create instance with the match_id field.

Leave a comment