class SurveyDataSerializer(serializers.ModelSerializer): id = serializers.SerializerMethodField() entry_id = serializers.SerializerMethodField() who_did_you_test_with = fields.MultipleChoiceField( choices=WHO_TESTED_CHOICES ) data_cleaning_techniques = fields.MultipleChoiceField( choices=CLEANING_TECHNIQUES_CHOICES ) class Meta: model = SurveyData fields = ( 'id', 'entry_id', 'have_you_tested_tool', 'who_did_you_test_with', 'considered_senstive', 'staff_trained', 'ask_sensitive', 'select_respondents', 'how_many_respondents', 'edit_sheet', 'data_cleaning_techniques', 'other_cleaning_technique' ) @classmethod def get_id(cls, obj): return str(obj.id) @classmethod def get_entry_id(cls, obj): return str(obj.id)
class BarSerializerLite(serializers.ModelSerializer): amenities = fields.MultipleChoiceField(choices=AMENITIES, required=False, allow_null=True, allow_blank=True) food_type = fields.MultipleChoiceField(choices=CUISINE, required=False, allow_null=True, allow_blank=True) class Meta: model = Bar fields = ['id', 'amenities', 'name', 'address', 'state', 'lga', 'phone_number', 'email', 'contact_name', 'contact_phone', 'designation', 'no_of_tables', 'no_of_waiting_staff', 'food_type', 'sample_type', 'date_captured', 'username', 'rating', 'lat', 'lng', 'image_1', 'image_2', 'image_3', 'image_4', 'image_5', 'remark', 'captured_time', 'branch' ]
class OrderModelSerializer(serializers.HyperlinkedModelSerializer): thai_choice = fields.MultipleChoiceField(choices=THAI_MENU) russian_choice = fields.MultipleChoiceField(choices=RUSSIAN_MENU) french_choice = fields.MultipleChoiceField(choices=FRENCH_MENU) class Meta: model = OrderModel fields = ('id', 'thai_choice', 'russian_choice', 'french_choice')
class PruebaSerializer(serializers.ModelSerializer): class Meta: model = Pelicula fields = ('idpelicula', 'titulo', 'horas', 'minutos', 'sinopsis', 'idioma', 'subtitulos', 'clase', 'namedire', 'apelldire', 'raking', 'imagen', 'vista', 'formatos', 'categoria') categoria = fields.MultipleChoiceField(choices=Categoria) formatos = fields.MultipleChoiceField(choices=Formatos)
class DiaryRecordingSerializer(serializers.ModelSerializer): """ DiaryRecording serializer """ tremor = fields.MultipleChoiceField(choices=TREMOR) brake = fields.MultipleChoiceField(choices=TREMOR) class Meta: model = DiaryRecording fields = '__all__'
class UserPrefSerializer(serializers.ModelSerializer): age = fields.MultipleChoiceField(choices=AGE_CHOICES) gender = fields.MultipleChoiceField(choices=GENDER_CHOICES) size = fields.MultipleChoiceField(choices=SIZE_CHOICES) class Meta: extra_kwargs = {'user': {'write_only': True}} fields = ( 'age', 'gender', 'size', ) model = models.UserPref
class PatientSerializer(serializers.ModelSerializer): medical_history = fields.MultipleChoiceField( choices=MEDICAL_HISTORY_CHOICES) class Meta: model = PatientRegistration fields = '__all__'
class PatientSerializer(serializers.ModelSerializer): medical_history = fields.MultipleChoiceField( choices=MEDICAL_HISTORY_CHOICES) class Meta: model = PatientRegistration exclude = ("created_by", "deleted")
class CourseDetailSerializer(serializers.HyperlinkedModelSerializer): tags = fields.MultipleChoiceField(TAGS) lessons = SerializerMethodField() reviews = SerializerMethodField() author = SerializerMethodField() # author = serializers.PrimaryKeyRelatedField(read_only=True) class Meta: model = Course fields = ('title', 'price', 'tags', 'author', 'caption', 'reviews', 'lessons', 'course_type', 'cover_photo', 'average_star_rating') def get_lessons(self, obj): review_queryset = Lesson.objects.filter(course=obj.id) lesson = LessonSerializer(review_queryset, many=True).data return lesson[0], lesson[1] def get_reviews(self, obj): # to get all the reviews on the course review_queryset = Reviews.objects.filter(course=obj.id) reviews = ReviewSerializer(review_queryset, many=True).data return reviews def get_author(self, obj): return (obj.author.username)
class JobSerializer(serializers.ModelSerializer): user = serializers.StringRelatedField() applicants = serializers.SerializerMethodField() freelancer = serializers.SerializerMethodField() technologies = fields.MultipleChoiceField(choices=TECHNOLOGIES) technologies_display = serializers.CharField(source='get_technologies_display', required=False) class Meta: model = Job fields = '__all__' read_only_fields = ['user', 'applicants', 'freelancer', 'technologies_display'] def get_applicants(self, obj): applicants = [] for a in obj.applicants.all(): applicant = { 'id': a.id, 'first_name': a.first_name, 'last_name': a.last_name, } applicants.append(applicant) return applicants def get_freelancer(self, obj): if obj.freelancer: return UserSerializer(obj.freelancer).data return None
class ItemSerializer(serializers.ModelSerializer): interest = fields.MultipleChoiceField(choices=ITEM_TYPES) user = UserSerializer(read_only=True) class Meta: model = Item fields = '__all__'
class UserProfileSerializer(serializers.HyperlinkedModelSerializer): user = UserSerializer(required=True) studytime = fields.MultipleChoiceField(choices=STUDY_TIMES) class Meta: model = UserProfile fields = ('pk', 'studytime', 'studylocation', 'user')
class HotelSerializerLite(serializers.ModelSerializer): amenities = fields.MultipleChoiceField(choices=AMENITIES) class Meta: model = Hotel fields = ['id', 'username', 'amenities', 'name', 'address', 'state', 'lga', 'phone_number', 'email', 'contact_name', 'contact_phone', 'designation', 'no_of_rooms', 'no_of_restaurants', 'date_captured', 'rating', 'lat', 'lng', 'image_1', 'image_2', 'image_3', 'image_4', 'image_5', 'remark', 'captured_time', 'branch' ]
class HotelSerializer(serializers.ModelSerializer): amenities = fields.MultipleChoiceField(choices=AMENITIES) user_id = serializers.IntegerField(required=False) user = UsersSerializer(required=False) class Meta: model = Hotel fields = ['id', 'amenities', 'name', 'address', 'state', 'lga', 'phone_number', 'email', 'contact_name', 'contact_phone', 'designation', 'no_of_rooms', 'no_of_restaurants', 'date_captured', 'user', 'user_id', 'rating', 'lat', 'lng', 'app_uid', 'image_1', 'image_2', 'image_3', 'image_4', 'image_5', 'remark', 'captured_time' ]
class OrganizerSerializer(serializers.ModelSerializer): music_type = fields.MultipleChoiceField(choices=choices.MUSIC_CHOICE) class Meta: model = Organizer fields = '__all__'
class RoomSerializer(serializers.ModelSerializer): owner = UserSerializer(many=False, read_only=True) features = fields.MultipleChoiceField(choices=Room.FEATURES, allow_blank=True) class Meta: model = Room fields = '__all__'
class GrupoSerializer(serializers.ModelSerializer): maestro = PersonaSerializer(many=True) dias = f.MultipleChoiceField(choices=fields.DAY_OF_THE_WEEK) class Meta: model = Programa.Grupo fields = ('id', 'dias', 'inicio', 'final', 'cupo', 'inicio', 'final', 'maestro')
class ExerciseSerializer(serializers.ModelSerializer): body_part = fields.MultipleChoiceField(choices=BODY_PART_CHOICES) class Meta: model = Exercise fields = ('id', 'name', 'difficulty', 'notes', 'body_part') read_only_fields = ('id', )
class EdmontonSerializer(serializers.ModelSerializer): q3_ind_func = fields.MultipleChoiceField(choices=MULTIPLE_CHOICES) class Meta: model = Edmonton fields = '__all__' read_only_fields = ('usuario', 'usuario_edit', 'fragilidade', 'fatores')
class ShopSerializer(serializers.HyperlinkedModelSerializer): services = fields.MultipleChoiceField(choices=SERVICE_CHOICES) class Meta: model = Shop fields = ('id', 'logo', 'name', 'type', 'phone', 'services', 'address', 'city', 'lat', 'long', 'isActive')
class AnnouncementSerializer(serializers.ModelSerializer): availability_display = serializers.SerializerMethodField() availability = fields.MultipleChoiceField(choices=Announcement.WEEKDAYS) announcement_thumbnail = VersatileImageFieldSerializer(sizes=[( 'full_size', 'url'), ('thumbnail', 'thumbnail__100x100'), ( 'medium_square_crop', 'crop__400x400'), ('small_square_crop', 'crop__50x50')]) job_tags = JobTagSerializer(many=True) announcement_images = AnnouncementImageSerializer(many=True) professional_id = serializers.PrimaryKeyRelatedField( source='professional', read_only=False, queryset=Professional.objects.all()) review_count = serializers.FloatField(read_only=True) review_average = serializers.FloatField(read_only=True) professional = ProfessionalAuxSerializer() class Meta: model = Announcement fields = ("id", "professional_rating_count", "professional_rating_average", "review_count", "review_average", "visible", "title", "description", "announcement_images", "job_tags", "professional", "professional_id", "price", "publish_date", "expire_date", "location", "approved", "availability", "availability_display", "movility", "announcement_thumbnail") depth = 3 def get_availability_display(self, obj): return obj.get_availability_display() def get_weekdays(self, obj): return obj.get_weekdays() def update(self, instance, validated_data): for attr, value in validated_data.items(): setattr(instance, attr, value) instance.save() return instance def create(self, validated_data): images_data = validated_data.pop('announcement_images') job_tags_data = validated_data.pop('job_tags') professional_data = validated_data.pop('professional') professional = Professional.objects.get(id=professional_data.id) announcement, created = Announcement.objects.get_or_create( professional=professional, **validated_data) for image_data in images_data: image, created = AnnouncementImage.objects.get_or_create( announcement=announcement, **image_data) for job_tag_data in job_tags_data: job_data = job_tag_data.pop('job') job = JobSubCategory.objects.get(id=job_data["id"]) job_tag, created = JobTag.objects.get_or_create( job=job, announcement=announcement, **job_tag_data) return announcement
class ProductDetailSerializer(ModelSerializer): topics = fields.MultipleChoiceField(TAGS) comments = SerializerMethodField() total_comments = SerializerMethodField() username = SerializerMethodField() user = SerializerMethodField() user_profile = HyperlinkedIdentityField(view_name='profile', lookup_field='pk') class Meta: model = Product fields = ( 'id', 'username', 'user', 'user_profile', 'url', 'name', 'topics', 'caption', 'upvote', 'thumbnail', 'download_link', 'status', 'twitter_url', 'content', 'launch_date', 'created_at', 'total_comments', 'comments', ) def get_comments(self, obj): queryset = Comment.objects.filter(product=obj.id) comments = CommentSerializer(queryset, many=True).data return comments def get_user(self, obj): return (obj.user.id) def get_username(self, obj): return (obj.user.username) def get_total_comments(self, obj): ''' this function gets the total replies and comments then add them up to give the overall comments ''' total = [] queryset = Comment.objects.filter(product=obj.id) comments = CommentSerializer(queryset, many=True).data for comment in comments: total.append(comment['total_replies']) total_replies = sum(total) total_comments = queryset.count() overall_comment = total_comments + total_replies return overall_comment
class StoreSerializer(serializers.ModelSerializer): service_detail = serializers.SerializerMethodField() working_days = fields.MultipleChoiceField(choices=WEEK_DAYS_CHOICES) class Meta: model = Store def get_service_detail(self, store): return map(values_from_queryset, store.services.all().values("name"))
class EventSerializer(serializers.ModelSerializer): topics = fields.MultipleChoiceField(choices=TOPIC_CHOICES) class Meta: model = Event fields = ('id', 'event_creator', 'title', 'date', 'topics', 'location', 'event_members', 'event_admins', 'slots', 'description', 'day') read_only_fields = ('event_creator', 'id')
class OrderSerializer(serializers.ModelSerializer): toppings = fields.MultipleChoiceField(choices=toppings_choices) id = serializers.IntegerField(read_only=True) class Meta: model = Order fields = [ 'id', 'pizza_type', 'pizza_size', 'toppings', 'cheese', 'no_of_pizza', 'cust_name' ]
class ShopSerializer(serializers.HyperlinkedModelSerializer): distance = serializers.DecimalField(source='distance.km', max_digits=10, decimal_places=2, required=False, read_only=True) services = fields.MultipleChoiceField(choices=SERVICE_CHOICES) class Meta: model = Shop geo_field = "location" fields = ('id', 'logo', 'name', 'type', 'phone', 'services', 'address', 'city', 'lat', 'long', 'location', 'distance', 'isActive')
class ApplicationSerializer(serializers.ModelSerializer): supervisions = SupervisionSerializer(many=True, required=False) tags = TagSerializer(many=True, required=False) academic_year = AcademicYearSerializer(read_only=True) academic_year_id = serializers.IntegerField() possible_funding = fields.MultipleChoiceField( choices=POSSIBLE_FUNDING_CHOICES) class Meta: model = Application fields = ('id', 'registry_ref', 'surname', 'forename', 'possible_funding', 'funding_status', 'origin', 'student_type', 'status', 'gender', 'research_subject', 'administrator_comment', 'phd_admission_tutor_comment', 'created_at', 'modified_at', 'supervisions', 'academic_year', 'academic_year_id', 'tags') def create(self, validated_data): return Application.objects.create( registry_ref=validated_data['registry_ref'], surname=validated_data['surname'], forename=validated_data['forename'], possible_funding=validated_data['possible_funding'], funding_status=validated_data['funding_status'], origin=validated_data['origin'], student_type=validated_data['student_type'], gender=validated_data['gender'], research_subject=validated_data['research_subject'], administrator_comment=validated_data['administrator_comment'], phd_admission_tutor_comment=validated_data[ 'phd_admission_tutor_comment'], academic_year_id=validated_data['academic_year_id']) def update(self, application, validated_data): application.registry_ref = validated_data['registry_ref'] application.surname = validated_data['surname'] application.forename = validated_data['forename'] application.possible_funding = validated_data['possible_funding'] application.funding_status = validated_data['funding_status'] application.origin = validated_data['origin'] application.student_type = validated_data['student_type'] application.status = validated_data['status'] application.gender = validated_data['gender'] application.research_subject = validated_data['research_subject'] application.administrator_comment = validated_data[ 'administrator_comment'] application.phd_admission_tutor_comment = validated_data[ 'phd_admission_tutor_comment'] application.academic_year_id = validated_data['academic_year_id'] application.save() return application
class DishSerializer(DefaultModelSerializer): """Serializer to map the Model instance into JSON format.""" seasons = fields.MultipleChoiceField(choices=SEASON_CHOICES) # dish_type = serializers.CharField(source='get_dish_type_display') class Meta: """Meta class to map serializer's fields with the model fields.""" model = Dish fields = ('id', 'name', 'dish_type', 'seasons', 'difficulty', 'priority', 'raw', 'is_post', 'is_diet', 'last_used_date')
class Mens_FormalShirtSerializers(serializers.ModelSerializer): MY_CHOICE = [('S', 'S'), ('M', 'M'), ('L', 'L'), ('XL', 'XL'), ('XXL', 'XXL')] class Meta: model = Mens_FormalShirt fields = ('id', 'brand', 'product_name', 'product_price', 'ideal_for', 'neck_type', 'seleeve_type', 'color', 'size', 'fit_type', 'seller_name', 'return_policy', 'cod', 'image', 'seller_name', 'no_of_ratings', 'avg_ratings') size = fields.MultipleChoiceField(choices=MY_CHOICE)
class CatSerializer(serializers.HyperlinkedModelSerializer): personality = fields.MultipleChoiceField(choices=PERSONALITY) class Meta: model = Cat fields = [ 'name', 'gender', 'age', 'description', 'breed', 'itype', 'status', 'arrival_date', 'arrival_details', 'medical_history', 'vaccinations', 'is_microchipped', 'flea_control_date', 'deworming_date', 'fiv_felv_date', 'special_needs', 'personality', 'more_personality', 'comments', 'personal_exp' ]