class HorarioServicioSerializer(serializers.ModelSerializer): fechaServicio = fields.DateField(input_formats=['%Y-%m-%d']) horaInicio = fields.TimeField(input_formats=['%H:%M:%S']) horaFin = fields.TimeField(input_formats=['%H:%M:%S']) class Meta: model = HorarioServicio fields = '__all__' def to_representation(self, instance): representation = super(HorarioServicioSerializer, self).to_representation(instance) representation['bus'] = { "idBus": instance.bus.idBus, "modelo": instance.bus.modelo, "patente": instance.bus.patente, "capacidadPasajeros": instance.bus.capacidadPasajeros } representation['chofer'] = { "idPersona": instance.chofer.idPersona, "identificacionPais": instance.chofer.identificacionPais, "nombrePrimario": instance.chofer.nombrePrimario, "nombreSecundario": instance.chofer.nombreSecundario, "apPaterno": instance.chofer.apPaterno, "apMaterno": instance.chofer.apMaterno } representation['trayecto'] = { "idTrayecto": instance.trayecto.idTrayecto, "ciudadSalida": instance.trayecto.ciudadSalida, "ciudadLlegada": instance.trayecto.ciudadLlegada } return representation
class Meta: param_fields = ( ('room_id', fields.IntegerField(label='会议室ID')), ('name', fields.CharField(label='名称', max_length=64)), ('description', fields.CharField(label='描述', max_length=255, required=False, default="")), ('date', fields.DateField(label='预定日期')), ('start_time', fields.TimeField(label='开始时间')), ('end_time', fields.TimeField(label='结束时间')), )
def test_basic_mapping(self, assert_dict_equals): """ Confirm that the serializer can still handle models w/ standard Django fields """ class TestSerializer(DjongoModelSerializer): class Meta: model = GenericModel fields = '__all__' expected_dict = { 'id': drf_fields.IntegerField(label='ID', read_only=True), 'big_int': drf_fields.IntegerField(max_value=9223372036854775807, min_value=-9223372036854775808), 'bool': drf_fields.BooleanField(), 'char': drf_fields.CharField(max_length=20), 'comma_int': ("CharField(validators=[<django.core.validators.RegexValidator " "object>, <django.core.validators.MaxLengthValidator object>])"), 'date': drf_fields.DateField(), 'date_time': drf_fields.DateTimeField(), 'decimal': drf_fields.DecimalField(decimal_places=5, max_digits=10), 'email': drf_fields.EmailField(max_length=254), 'float': drf_fields.FloatField(), 'integer': drf_fields.IntegerField(max_value=2147483647, min_value=-2147483648), 'null_bool': drf_fields.NullBooleanField(required=False), 'pos_int': drf_fields.IntegerField(max_value=2147483647, min_value=0), 'pos_small_int': drf_fields.IntegerField(max_value=32767, min_value=0), 'slug': drf_fields.SlugField(allow_unicode=False, max_length=50), 'small_int': drf_fields.IntegerField(max_value=32767, min_value=-32768), 'text': "CharField(style={'base_template': 'textarea.html'})", 'time': drf_fields.TimeField(), 'url': drf_fields.URLField(max_length=200), 'ip': drf_fields.IPAddressField(), 'uuid': "ModelField(model_field=<django.db.models.fields.UUIDField: uuid>)", } assert_dict_equals(TestSerializer().get_fields(), expected_dict)