class MarathonSerializer(DynamicFieldsMixin, serializers.ModelSerializer): organizer = NestedField(UserSerializer, read_only=True, fields=['full_name']) sponsors = NestedField(SponsorSerializer, many=True, required=False, fields=['name'], create_ops=['create'], update_ops=['create', 'remove', 'update']) categories = NestedField(CategorySerializer, required=True, exclude=['marathon'], create_ops=['create'], update_ops=['create', 'remove', 'update']) class Meta: model = Marathon fields = ( 'id', 'url', 'name', 'theme', 'organizer', 'sponsors', 'categories', 'start_date', 'end_date', ) def create(self, validated_data): request = self.context.get('request') user = request.user validated_data.update({'organizer': user}) return super().create(validated_data)
class WritableStudentWithAliasSerializer(DynamicFieldsMixin, NestedModelSerializer): program = NestedField(WritableCourseSerializer, source="course", allow_null=True, required=False) contacts = NestedField(PhoneSerializer, source="phone_numbers", many=True, required=False) class Meta: model = Student fields = ['name', 'age', 'program', 'contacts']
class WritableStudentWithAliasSerializer(DynamicFieldsMixin, NestedModelSerializer): program = NestedField(WritableCourseSerializer, source="course", allow_null=True, required=False) contacts = NestedField(PhoneSerializer, source="phone_numbers", many=True, required=False) study_partner = NestedField('self', required=False, allow_null=True, accept_pk=True, exclude=['study_partner']) sport_mates = NestedField('self', required=False, many=True, source="sport_partners", exclude=['sport_mates']) class Meta: model = Student fields = [ 'name', 'age', 'program', 'contacts', 'study_partner', 'sport_mates' ]
class WritableStudentSerializer(NestedModelSerializer): course = NestedField(WritableCourseSerializer) phone_numbers = NestedField(PhoneSerializer, many=True, required=False) class Meta: model = Student fields = ['name', 'age', 'course', 'phone_numbers']
class ReplaceableStudentWithAliasSerializer(DynamicFieldsMixin, NestedModelSerializer): full_name = serializers.CharField(source="name") program = NestedField(WritableCourseSerializer, source="course", accept_pk_only=True, allow_null=True, required=False) contacts = NestedField(PhoneSerializer, source="phone_numbers", many=True, required=False) class Meta: model = Student fields = ['full_name', 'age', 'program', 'contacts']
class WritableStudentSerializer(DynamicFieldsMixin, NestedModelSerializer): course = NestedField(WritableCourseSerializer, allow_null=True, required=False) phone_numbers = NestedField(PhoneSerializer, many=True, required=False, allow_remove_all=True) class Meta: model = Student fields = ['name', 'age', 'course', 'phone_numbers']
class WritableCourseSerializer(DynamicFieldsMixin, NestedModelSerializer): books = NestedField(WritableBookSerializer, many=True, required=False, allow_remove_all=True) instructor = NestedField(InstructorSerializer, accept_pk=True, required=False) class Meta: model = Course fields = ['name', 'code', 'books', 'instructor']
class ReplaceableStudentSerializer(DynamicFieldsMixin, NestedModelSerializer): course = NestedField(WritableCourseSerializer, accept_pk=True, allow_null=True, required=False) phone_numbers = PhoneSerializer(many=True, read_only=True) class Meta: model = Student fields = ['name', 'age', 'course', 'phone_numbers']
class ReplaceableStudentSerializer(NestedModelSerializer): course = NestedField(WritableCourseSerializer, accept_pk=True) phone_numbers = PhoneSerializer(many=True, read_only=True) class Meta: model = Student fields = ['name', 'age', 'course', 'phone_numbers']
class ReplaceableCourseSerializer(NestedModelSerializer): books = NestedField(BookSerializer, accept_pk=True, many=True, required=False) class Meta: model = Course fields = ['name', 'code', 'books']
class WritableBookSerializer(DynamicFieldsMixin, NestedModelSerializer): genre = NestedField(GenreSerializer, many=False, required=False, allow_null=True, accept_pk=True) class Meta: model = Book fields = ['title', 'author', 'genre']
class HostelSerializer(PropertySerializer): rooms = NestedField(RoomSerializer, many=True, required=False, exclude=['property']) class Meta: model = Hostel fields = ('rooms', ) Meta.fields = PropertySerializer.Meta.fields + Meta.fields
class SingleRoomSerializer(PropertySerializer): rooms = NestedField(RoomSerializer, many=True, required=False, exclude=['property']) class Meta: model = SingleRoom fields = ( 'rooms_count', 'rooms', ) Meta.fields = PropertySerializer.Meta.fields + Meta.fields
class UserSerializer(DynamicFieldsMixin, NestedModelSerializer): picture = ProfilePictureSerializer(read_only=True) password = serializers.CharField(write_only=True, style={'input_type': 'password'}) fav_properties = NestedField(MinimalPropertySerializer, many=True, return_pk=True, write_only=True, create_ops=[], update_ops=['add', 'remove'], required=False) class Meta: model = User fields = ('id', 'url', 'username', 'email', 'password', 'phone', 'groups', 'date_joined', 'is_staff', 'full_name', 'is_active', 'picture', 'biography', 'fav_properties') read_only_fields = ('date_joined', 'is_staff')
class PropertySerializer(DynamicFieldsMixin, NestedModelSerializer): pictures = PropertyPictureSerializer(many=True, read_only=True) location = NestedField(LocationSerializer, many=False) amenities = NestedField(AmenitySerializer, many=True, required=False) services = NestedField(ServiceSerializer, many=True, required=False) potentials = NestedField(PotentialSerializer, many=True, required=False) contact = NestedField(ContactSerializer, many=False, required=False) other_features = NestedField(FeatureSerializer, many=True, required=False, create_ops=["create"], update_ops=["update", "create", "remove"]) owner = UserSerializer(many=False, read_only=True, exclude=['fav_properties']) is_my_favourite = serializers.SerializerMethodField() type = serializers.CharField(read_only=True) # This is used when retrieving nearby properties distance = serializers.CharField(default=None) class Meta: model = Property fields = ('id', 'url', 'type', 'available_for', 'available_for_options', 'price', 'price_rate_unit', 'payment_terms', 'is_price_negotiable', 'rating', 'currency', 'descriptions', 'location', 'owner', 'amenities', 'services', 'potentials', 'pictures', 'other_features', 'contact', 'post_date', 'is_my_favourite', 'distance') def get_is_my_favourite(self, obj): request = self.context.get('request') user = request.user if user.is_authenticated: return user.fav_properties.all().filter(id=obj.id).exists() return False def create(self, validated_data): """function for creating a property """ request = self.context.get('request') user = request.user validated_data.update({"owner": user}) property = super().create(validated_data) return property
class RoomSerializer(DynamicFieldsMixin, serializers.ModelSerializer): type = NestedField(RoomTypeSerializer, accept_pk=True, required=False) class Meta: model = Room fields = ('id', 'property', 'type', 'count')
class WritableCourseSerializer(DynamicFieldsMixin, NestedModelSerializer): books = NestedField(WritableBookSerializer, many=True, required=False) class Meta: model = Course fields = ['name', 'code', 'books']
class ArticleSerializer(DynamicFieldsMixin, NestedModelSerializer): article_comments = NestedField(ArticleCommentSerializer, many=True, exclude = ['id'], required=False) class Meta: model = Articles fields = ['id', 'author', 'title', 'description', 'article_comments']
class WritableBookSerializer(DynamicFieldsMixin, NestedModelSerializer): genres = NestedField(GenreSerializer, many=True, required=False) class Meta: model = Book fields = ['title', 'author', 'genres']