how do you automatically fill this field (bizuser)ForeignKey rather than having to look for bizuser ID

Viewed 30

MY model

class XP(models.Model):
 bizuser = models.ForeignKey(BizUser, on_delete=models.CASCADE)
 current_xp_price = models.IntegerField(default=0)
 xp_created_at = models.DateTimeField(auto_now_add=True)
 xp_updated_at = models.DateTimeField(auto_now_add=True)

i have this function that creates an XP i want if the current user is authenticated(jwttokens) the user can then go ahead to create the XP .when i go to post this data i get to fields that i have to fill that is current_xp_price and bizuser which is a ForeignKey

Views

class create_xp(APIView):
def post(self, request):
    if request.user.is_authenticated:
        current_user = request.BizUser
        serializer = XPSerializer(data=request.data)
        request.data["bizuser"] = current_user.id

        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
    else:
        return Response(status=status.HTTP_401_UNAUTHORIZED)

serializers

class XPSerializer(serializers.ModelSerializer):
class Meta:
    model = XP
    fields = ['current_xp_price', ]
0 Answers
Related