Dashboard-XRoom/core/serializers/SubscriptionSerializer.py
2025-06-07 17:20:41 +03:30

22 lines
944 B
Python

from rest_framework import serializers
from core.models.Subscription import Subscription
class SubscriptionSerializer(serializers.ModelSerializer):
class Meta:
model = Subscription
fields = ['user', 'license_number', 'user_count', 'startTime', 'endTime', 'price' , 'id']
read_only_fields = ['user'] # user will be set automatically by the view
def create(self, validated_data):
"""
Override the create method to ensure the user is assigned automatically.
This is important since 'user' is not part of the data coming from the client
but is assigned based on the authenticated user.
"""
# The user is automatically added by the view (request.user)
user = validated_data.pop('user', None) # We do not expect user to be passed from the client
subscription = Subscription.objects.create(**validated_data, user=user)
return subscription