예제 #1
0
class TokenSerializer(serializers.ModelSerializer):
    allowed_subnets = serializers.ListField(
        child=netfields_rf.CidrAddressField(), required=False)
    token = serializers.ReadOnlyField(source='plain')
    is_valid = serializers.ReadOnlyField()

    class Meta:
        model = models.Token
        fields = (
            'id',
            'created',
            'last_used',
            'max_age',
            'max_unused_period',
            'name',
            'perm_manage_tokens',
            'allowed_subnets',
            'is_valid',
            'token',
        )
        read_only_fields = ('id', 'created', 'last_used', 'token')

    def __init__(self, *args, include_plain=False, **kwargs):
        self.include_plain = include_plain
        return super().__init__(*args, **kwargs)

    def get_fields(self):
        fields = super().get_fields()
        if not self.include_plain:
            fields.pop('token')
        return fields
 class TestSerializer(serializers.Serializer):
     cidr = fields.CidrAddressField()
예제 #3
0
 class TestSerializer(serializers.Serializer):
     ip = fields.CidrAddressField(validators=[validate])